Android封装OkHttpClient的类库

时间:2021-09-26 21:55:53

由于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();

}
}

 Android封装OkHttpClient的类库