• Android封装OkHttpClient的类库


    由于android6.0的SDK没有HttpClient,只有HttpURLConnection和OkHttpClient,特记下OkHttpClient的使用方法

    1.Ui测试界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context="com.example.lidezhen.httpdemo.MainActivity">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Get方式"
            android:id="@+id/button1"
            android:onClick="Button1_Click"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Post方式"
            android:id="@+id/button2"
            android:onClick="Button2_Click"/>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/text1"
                android:hint="这是内容"/>
        </ScrollView>
    </LinearLayout>

    添加权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.lidezhen.httpdemo">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">

    <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

    </activity>

    </application>

    </manifest>

    代码

    package com.example.lidezhen.httpdemo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    
    import okhttp3.FormBody;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    public class MainActivity extends AppCompatActivity {
    
        TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView= (TextView) findViewById(R.id.text1);
    
        }
    
    
    
    
        public void Button1_Click(View v)  {
            new  Thread(new Runnable() {
                  @Override
                  public void run() {
                      String url="https://www.baidu.com/";
                      OkHttpClient client = new OkHttpClient();
    
                      Request request = new Request.Builder()
                              .url(url).build();
    
                      Response response = null;
                      try {
                          response = client.newCall(request).execute();
    
                          if (response.isSuccessful()) {
    
                              SetText( response.body().string());
    
                          } else {
                              SetText( "Unexpected code " + response);
    
                          }
                      }
                      catch (Exception e)
                      {
                          SetText( "获取网页失败");
                      }
                  }
    
    
              }).start();
    
        }
        public void SetText(final String s)
        {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(s);
                }
            });
        }
    
        public void Button2_Click(View v)
        {
            new  Thread(new Runnable() {
                @Override
                public void run() {
                    String url="https://passport.baidu.com/v2/api/?login";
                    OkHttpClient client = new OkHttpClient();
                    FormBody.Builder formBody = new FormBody.Builder();
                    formBody.add("username","abc123").add("password","123456");
                    Request request = new Request.Builder()
                            .post(formBody.build())
                            .url(url).build();
    
                    Response response = null;
                    try {
                        response = client.newCall(request).execute();
    
                        if (response.isSuccessful()) {
    
                            SetText( response.body().string());
    
                        } else {
                            SetText( "Unexpected code " + response);
    
                        }
                    }
                    catch (Exception e)
                    {
                        SetText( "获取网页失败");
                    }
                }
    
    
        }).start();
    
    }
    }

     

  • 相关阅读:
    docker相关资源监控采集方法整理
    三节课MINI计划第四周
    三节课MINI计划第五周
    三节课MINI计划第四周
    三节课MINI计划第三周
    三节课MINI计划第二周
    Bilibili用户需求分析报告
    三节课MINI计划第一周
    《产品思维30讲 》学习笔记
    PHP-FPM未授权访问漏洞
  • 原文地址:https://www.cnblogs.com/lidezhen/p/5713234.html
Copyright © 2020-2023  润新知