okhttp的使用
一:OkHttpClient依赖
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
二:okhttp完成get请求

//网络请求json串下载
public void getjson(String url){
/**
* okHttpClient: build()出来,作用:关联Request请求,关联Call
* Request: build()出来,设置请求方式,网址
* Call:执行请求 方法:失败,成功response.body().string();
*/
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(5, TimeUnit.SECONDS);//读取超时
builder.connectTimeout(5,TimeUnit.SECONDS);//连接超时
OkHttpClient client = builder.build();//获得一个客户端对象
Request request = new Request.Builder().get().url(url).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败;
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功
json = response.body().string();
Log.i(TAG, "onResponse: 内容"+json);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "内容:"+json, Toast.LENGTH_SHORT).show();
}
});
}
});
}
三:okhttp完成post请求

//post上传
public void postjson(HashMap<String,String> body, String url){
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();
FormBody.Builder formBody = new FormBody.Builder();
for (String k:body.keySet()){
formBody.add(k,body.get(k));
}
FormBody build = formBody.build();
Request request = new Request.Builder().url(url).post(build).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "onFailure: 上传失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.i(TAG, "onResponse: 上传成功,结果:"+string);
}
});
}
四:okhttp完成上传文件

//post上传音乐
public void postMedia(String url){
OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.SECONDS).connectTimeout(5, TimeUnit.SECONDS).build();
MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "test.mp3", RequestBody.create(MediaType.parse("media.mp3"), new File("/mnt/sdcard/2.jpg"))).build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "onFailure: 上传失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.i(TAG, "onResponse: 成功:"+string);
}
});
五:拦截器
1.什么是拦截器
拦截网络操作
2.拦截器种类
桥、连接、日志、服务、缓存等
3.拦截器实际应用
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i(TAG, "log: "+message);
}
});
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(5, TimeUnit.SECONDS);//读取超时
builder.connectTimeout(5,TimeUnit.SECONDS);//连接超时
builder.addInterceptor(interceptor);
消息头
//添加消息头
public void addHeader(){
//拦截器
interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("token","1").build();
return chain.proceed(request);//开始拦截
}
};
}
779

被折叠的 条评论
为什么被折叠?



