添加OkHttp依赖
implementation \'com.squareup.okhttp3:okhttp:3.11.0\'
implementation \'com.squareup.okhttp3:logging-interceptor:3.12.0\'
Utils类
package com.***.***.util;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class HttpUtils {
public static String get(String urlString){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
Request request = new Request.Builder().url(urlString).get().build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i(\"dt\",\"请求结果:\"+result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return \"\";
}
public static String postForm(String url,String[] name,String[] value){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
FormBody.Builder formBuild = new FormBody.Builder();
for (int i = 0; i < name.length; i++) {
formBuild.add(name[i],value[i]);
}
Request request = new Request.Builder().url(url).post(formBuild.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i(\"dt\",result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return \"\";
}
public static String postFile(String url,String[] name,String[] value,String fileParamName,File file){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
if(file != null){
// MediaType.parse() 里面是上传的文件类型。
RequestBody body = RequestBody.create(MediaType.parse(\"image/*\"), file);
String filename = file.getName();
// 参数分别为: 文件参数名 ,文件名称 , RequestBody
requestBody.addFormDataPart(fileParamName, \"jpg\", body);
}
if (name!=null) {
for (int i = 0; i < name.length; i++) {
requestBody.addFormDataPart(name[i], value[i]);
}
}
Request request = new Request.Builder().url(url).post(requestBody.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.code()==200) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return \"\";
}
public static String postJson(String url,String jsonString){
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse(\"application/json\"),jsonString);
Request request = new Request.Builder().url(url).post(requestBody).build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return \"\";
}
}
LoggingInterceptor类
package com.***.***.util;
import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
*
*
* 打印OKHttp请求日志
*/
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
//这个chain里面包含了request和response,所以你要什么都可以从这里拿
Request request = chain.request();
long t1 = System.nanoTime();//请求发起的时间
Log.i(\"dt\",String.format(\"发送请求 %s on %s%n%s\",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();//收到响应的时间
//这里不能直接使用response.body().string()的方式输出日志
//因为response.body().string()之后,response中的流会被关闭,程序会报错,我们需要创建出一
//个新的response给应用层处理
ResponseBody responseBody = response.peekBody(1024 * 1024);
Log.i(\"dt\",String.format(\"接收响应: [%s] %n返回json:【%s】 %.1fms%n%s\",
response.request().url(),
responseBody.string(),
(t2 - t1) / 1e6d,
response.headers()));
return response;
}
}
继续阅读与本文标签相同的文章
上一篇 :
神经网络到底要做多少层?沈向洋专访
下一篇 :
往ftp服务器上传文件
-
最佳 Linux 发行版汇总
2026-05-18栏目: 教程
-
StartDT AI Lab | 视觉智能引擎——AI识货赋能商品数字化
2026-05-18栏目: 教程
-
【DockerCon2017技术解读】如何在阿里云一键部署高可用的Kubernetes集群
2026-05-18栏目: 教程
-
基于Jenkins的开发测试全流程持续集成实践
2026-05-18栏目: 教程
-
什么是网络爬虫?有什么用?怎么爬?终于有人讲明白了
2026-05-18栏目: 教程
