1. OKHttp,好用的Http服务
官网地址:http://square.github.io/okhttp/
1) 获取http文件内容
1 OkHttpClient client = new OkHttpClient(); 2 3 String run(String url) throws IOException { 4 Request request = new Request.Builder() 5 .url(url) 6 .build(); 7 8 Response response = client.newCall(request).execute(); 9 return response.body().string(); 10 }
2)发送POST请求
1 public static final MediaType JSON 2 = MediaType.parse("application/json; charset=utf-8"); 3 4 OkHttpClient client = new OkHttpClient(); 5 6 String post(String url, String json) throws IOException { 7 RequestBody body = RequestBody.create(JSON, json); 8 Request request = new Request.Builder() 9 .url(url) 10 .post(body) 11 .build(); 12 Response response = client.newCall(request).execute(); 13 return response.body().string(); 14 }
时间: 2024-10-10 01:41:38