1、加入对okhttp的依赖
在app的build.gradle当中的
dependencies{
}标签中加入依赖
compile 'com.squareup.okhttp3:okhttp:3.3.1'
2、okhttpclient
client = new OkHttpClient();
需要新建立一个子线程,对于超过1MB的响应body,应使用流的方式来处理body。
private void syncGet() {
new Thread() {
@Override
public void run() {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
Log.d(TAG, responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
Log.d(TAG, "body===>" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
结果:4、异步http get
private void asynchronousGet() {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
Log.d(TAG, responseHeaders.name(i) + "=====> " + responseHeaders.value(i));
}
Log.d(TAG, "body===>" + response.body().string());
}
});
}
5、提取响应头
private void getresponseHeader() {
new Thread() {
@Override
public void run() {
Request request = new Request.Builder()
.url("https://api.github.com/repos/square/okhttp/issues")
.header("User-Agent", "OkHttp Headers.java")
.addHeader("Accept", "application/json; q=0.5")
.addHeader("Accept", "application/vnd.github.v3+json")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Log.d(TAG, "Server: " + response.header("Server"));
Log.d(TAG, "Date: " + response.header("Date"));
Log.d(TAG, "Vary: " + response.headers("Vary"));
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
07-04 11:49:25.644 4300-4340/com.example.okhttpdemo D/MainActivity: Server: GitHub.com 07-04 11:49:25.644 4300-4340/com.example.okhttpdemo D/MainActivity: Date: Tue, 04 Jul 2017 03:49:23 GMT 07-04 11:49:25.644 4300-4340/com.example.okhttpdemo D/MainActivity: Vary: [Accept, Accept-Encoding, Accept-Encoding]
6、http post提交String
private void postString() {
new Thread() {
@Override
public void run() {
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Call call = client.newCall(request);
Response response = null;
try {
response = call.execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Log.d(TAG, "body====>" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
7、post stream
private void postStream() {
new Thread() {
@Override
public void run() {
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
return MEDIA_TYPE_MARKDOWN;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("Numbers\n");
sink.writeUtf8("-------\n");
for (int i = 2; i <= 997; i++) {
sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
}
}
private String factor(int n) {
for (int i = 2; i < n; i++) {
int x = n / i;
if (x * i == n)
return factor(x) + " × " + i;
}
return Integer.toString(n);
}
};
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(requestBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Log.d(TAG, "body--->" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
8、post file
我在android手机根目录下放置了一个file.txt文件,文件里面的内容是123456
private void postFile() {
new Thread() {
@Override
public void run() {
File file = new File("/mnt/sdacard/360sicheck.txt");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Log.d(TAG, "body===>" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
打印结果:
07-04 13:42:27.510 25669-25794/com.example.okhttpdemo D/MainActivity: body===>123456
9、post提交表单
private void postForm() {
new Thread() {
@Override
public void run() {
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
Request request = new Request.Builder()
.url("https://en.wikipedia.org/w/index.php")
.post(formBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Log.d(TAG, "body===>" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
打印结果:
07-04 13:50:17.949 1822-2120/com.example.okhttpdemo D/MainActivity: body===>Jurassic Park - Wikipedia