首先记得在build.gradle 和 配置文件分别加上依赖 和 网络权限
implementation'com.squareup.okhttp3:okhttp:3.9.1'
<uses-permission android:name="android.permission.INTERNET" />
下面是具体代码
package com.example.okhttpdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private TextView tvResult;
//测试接口
private String strGet = "http://www.baidu.com/";
private String strPost = "http://api.apiopen.top/likePoetry";
//1.拿到okHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tv_result);
}
public void doGet(View view) {
//1.拿到okHttpClient对象
//2.构造Request
Request.Builder builder = new Request.Builder();
Request request = builder.get().url(strGet).build();
//3.将Request封装位call
Call call = okHttpClient.newCall(request);
//4.执行call
//Response response = call.execute();//同步
call.enqueue(new Callback() {//异步
@Override
public void onFailure(Call call, IOException e) {
L.e("onFailure:" + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
L.e("onResponse:" + res);
runOnUiThread(new Runnable() {//异步在子线程,不允许直接显示
@Override
public void run() {
tvResult.setText(res);
}
});
}
});
}
public void doPost(View view) {
FormBody formBody = new FormBody.Builder().add("name", "李白").build();
//1.拿到okHttpClient对象
//2.构造Request
Request.Builder builder = new Request.Builder();
Request request = builder.url(strPost).post(formBody).build();
//3.将Request封装位call
Call call = okHttpClient.newCall(request);
//4.执行call
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
L.e("onFailure:" + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
L.e("onResponse:" + res);
runOnUiThread(new Runnable() {//异步在子线程,不允许直接显示
@Override
public void run() {
tvResult.setText(res);
}
});
}
});
}
}
<?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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doGet"
android:text="GET" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doPost"
android:text="POST" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"/>
</LinearLayout>