Http请求
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 \"\";
}
}
自定义类
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;
}
}
继续阅读与本文标签相同的文章
上一篇 :
揭秘黄牛:为啥实名制也挡不住黄牛?
下一篇 :
4G用户过渡到5G到底是换手机还是换SIM卡?
-
2019云栖大会 | 究竟哪款NoSQL数据库最适合你?
2026-05-18栏目: 教程
-
CNCF 宣布成立应用交付领域小组,正式开启云原生应用时代
2026-05-18栏目: 教程
-
蚂蚁金服体验科技精选1-3期
2026-05-18栏目: 教程
-
9月新规1天顶平时1个月的收入,消费再少也有返利? 再创日赚万元的日子迎接双11
2026-05-18栏目: 教程
-
《Apache Kafka实战》| 每日读本书
2026-05-18栏目: 教程
