作者:魔豆从容_368 | 来源:互联网 | 2023-10-13 18:17
对okhttp网络请求的简单介绍
public class App extends Application {
//OkHttpClient实例是唯一的, 所有的请求都会通过这个OkHttpClient,所以所有的请求都可能被拦截器拦截,
// 我们可以在这个必经之路,做一些通用的操作,比如打印日志.
private static OkHttpClient okHttpClient;
@Override
public void onCreate() {
super.onCreate();
//建议一个app只有一个OkHttpClient实例
okHttpClient = new OkHttpClient();
okHttpClient = okHttpClient.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.addInterceptor(new MyLogInterceptor())
.build();
}
public static OkHttpClient okHttpClient() {
return okHttpClient;
}
//拦截器,可以修改header,可以通过拦截器打印日志
public class MyLogInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.header("shenfen", "chinesse")
.build();
HttpUrl url = request.url();
String httpUrl = url.url().toString();
Log.e("TAG", "============" + httpUrl);
Response respOnse= chain.proceed(request);
int code = response.code();
Log.e("TAG", "============response.code() == " + code);
return response;
}
}
}
Activity类
public class MainActivity extends AppCompatActivity {
private OkHttpClient okHttpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okHttpClient = App.okHttpClient();
}
//同步的get
public void get(View view) {
//request 设置url
final Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
new Thread(new Runnable() {
@Override
public void run() {
//通过newCall方法将request转换成call ,如果用execute()是同步执行
try {
Response respOnse= okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
//通过response.body().string()拿到服务器给我们的json
// String json = response.body().string();
//通过response.body().inputStream拿到服务器给我们的输入流 -- 主要用在大文件
// InputStream inputStream = response.body().byteStream();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get成功", Toast.LENGTH_SHORT).show();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();
}
});
}
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
//异步,所以的回调方法里面都是分线程.不能更新ui
public void getAsync(View view) {
//request 设置url
final Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String json = response.body().string();
//用Gson解析
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get成功", Toast.LENGTH_SHORT).show();
}
});
} else {
//提示用户
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
//Post异步请求
public void postAsync(View view) {
FormBody formBody = new FormBody.Builder()
.add("type", "yuantong")
.add("postid", "11111111111")
.build();
final Request request = new Request.Builder()
.url(" http://www.kuaidi100.com/query")
.post(formBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "post失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String json = response.body().string();
//用Gson解析
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "post成功", Toast.LENGTH_SHORT).show();
}
});
} else {
//提示用户
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "get失败", Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
}