今天我们就讲一下okhttp的使用,具体的okhttp使用可以参见官方的文档。

okhttp的使用

一、okhttp的下载安装

  • Download the latest JAR or grab via Maven:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.5.0</version>
</dependency>
  • 或者使用Gradle:在build.properties的dependencies下添加:
compile 'com.squareup.okhttp3:okhttp:3.5.0'

二、okHttp的基础使用

  • get的同步请求:The string() method on response body is convenient and efficient for small documents. But if the response body is large (greater than 1 MiB), avoid string() because it will load the entire document into memory. In that case, prefer to process the body as a stream.
@Test
public void okHttpTest1() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/ParameterServlet").build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}
  • get的异步请求:Download a file on a worker thread, and get called back when the response is readable. The callback is made after the response headers are ready. Reading the response body may still block. OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:9999/huhx1.json").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("hello wrold fail");
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
} @Override
public void onFailure(Call call, IOException exception) {
exception.printStackTrace();
}
});
  • post请求
@Test
public void okHttpTest3() {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder().add("username", "huhx").add("password", "123456").build();
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/ParameterServlet").post(formBody).build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}
  • json格式的post请求
@Test
public void okHttpTest4() {
MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
json.put("username", "huhx");
json.put("password", "123456");
RequestBody requestBody = RequestBody.create(JSON_TYPE, json.toJSONString());
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/JsonRequestServlet").post(requestBody).build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}

友情链接

04-29 03:31