okhttp網路請求框架的簡單使用
同步GET請求:
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
非同步GET請求:
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
client.newCall(request).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, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
});
}
獲取頭部:
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
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 = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println("Server: " + response.header("Server"));
System.out.println("Date: " + response.header("Date"));
System.out.println("Vary: " + response.headers("Vary"));
}
POST的請求String:
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
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();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
POST的請求Stream:public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
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 = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
POST的請求File:
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
File file = new File("README.md");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
更多內容請檢視官網:https://github.com/square/okhttp/wiki/Recipes https://github.com/square/retrofit
相關文章
- Android網路請求(3) 網路請求框架OkHttpAndroid框架HTTP
- Flutter學習(7)——網路請求框架Dio簡單使用Flutter框架
- OKHttp網路請求原理流程解析HTTP
- Flutter 熱門網路請求框架Dio的簡單封裝Flutter框架封裝
- Android網路請求(4) 網路請求框架VolleyAndroid框架
- Android網路請求(終) 網路請求框架RetrofitAndroid框架
- 淺析okHttp3的網路請求流程HTTP
- flutter網路請求框架dio基本使用Flutter框架
- OkHttp、rxJava、Retrofit聯合網路請求(二)HTTPRxJava
- OkHttp、rxJava、Retrofit聯合網路請求(一)HTTPRxJava
- Android使用Kotlin+Retrofit+Rxjava實現簡單的網路請求AndroidKotlinRxJava
- 簡單6步搞定Flutter網路請求Flutter
- 安卓okhttp3進行網路請求,一個簡單的登入頁面的實現安卓HTTP
- Flutter 網路請求的三種簡單實現Flutter
- Kotlin中Retrofit網路請求簡單封裝Kotlin封裝
- MVVM框架的搭建(三)——網路請求MVVM框架
- Retrofit+okhttp+Rxjava封裝網路請求工具類HTTPRxJava封裝
- 從網路請求過程看OkHttp攔截器HTTP
- Flutter 網路請求框架封裝Flutter框架封裝
- Volley 網路請求框架介紹與使用說明框架
- 簡單的聊聊網路請求中的記憶體拷貝記憶體
- 簡單介紹shell中的curl網路請求的實現
- iOS 使用Moya網路請求iOS
- OKHttp3簡單使用HTTP
- axios躺坑之路:cookie,簡單請求與非簡單請求。iOSCookie
- 使用retrofit進行網路請求
- Andriod 網路框架 OkHttp 原始碼解析框架HTTP原始碼
- Okhttp同步請求原始碼分析HTTP原始碼
- Retrofit + Kotlin + MVVM 的網路請求框架的封裝嘗試KotlinMVVM框架封裝
- 如何使用 Python 請求網路資源Python
- 網路請求了
- HttpSender OkHttp+RxJava超好用、功能超級強大的Http請求框架HTTPRxJava框架
- 網路請求優化之取消請求優化
- ios-APP重構之路(一) 網路請求框架iOSAPP框架
- OkHttp簡單剖析HTTP
- OkHttp 原始碼分析(一)—— 請求流程HTTP原始碼
- Http請求-okhttp3基本用法HTTP
- 使用Retrofit+RxJava實現網路請求RxJava
- vue(24)網路請求模組axios使用VueiOS