★37.OkHttp
2798 ワード
概要
公式サイト
単純な例
0.OkHttp導入
import okhttp3.*;
1.OkHttpClientの作成
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(9, TimeUnit.SECONDS)
.build();
2.Requestの作成
final Request request = new Request.Builder()
.client(okHttpClient)
.url("https://www.baidu.com/")
.build();
3.Callの作成
Call call = okHttpClient.newCall(request);
4.実行Call
非同期実行
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
byte[] bytes = response.body().bytes();
// , Android HTTP API, 。
ByteArrayInputStream inputStream = (ByteArrayInputStream) response.body().byteStream();
runOnUiThread(new Runnable() {
@Override
public void run() {
// UI
}
});
}
});
同期実行
try {
Response response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
POST例
1.RequestBodyの作成
タイプ1:FormBody
RequestBody body1 = new FormBody.Builder().add("username", "name").build();
タイプ2:MultipartBody
File file = new File(Environment.getExternalStorageDirectory(), "balabala.mp4");
RequestBody body2 = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("name1", "value1")
//
.addFormDataPart("name2", "filename1", RequestBody.create(MediaType.parse("image/png"), file))
.addPart(Headers.of( "Content-Disposition", "form-data; name=\"name3\""),
RequestBody.create(null, "value2"))
//
.addPart(Headers.of("Content-Disposition", "form-data; name=\"name4\"; filename =\"filename2\""),
RequestBody.create(MediaType.parse("image/png"), file))
.build();
2.Requestの作成
Request request = new Request.Builder()
.url("https://www.baidu.com/")
.post(/* RequestBody */)
.build();
注意事項
0.OkHttp導入
import okhttp3.*;
1.OkHttpClientの作成
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(9, TimeUnit.SECONDS)
.build();
2.Requestの作成
final Request request = new Request.Builder()
.client(okHttpClient)
.url("https://www.baidu.com/")
.build();
3.Callの作成
Call call = okHttpClient.newCall(request);
4.実行Call
非同期実行
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
byte[] bytes = response.body().bytes();
// , Android HTTP API, 。
ByteArrayInputStream inputStream = (ByteArrayInputStream) response.body().byteStream();
runOnUiThread(new Runnable() {
@Override
public void run() {
// UI
}
});
}
});
同期実行
try {
Response response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
POST例
1.RequestBodyの作成
タイプ1:FormBody
RequestBody body1 = new FormBody.Builder().add("username", "name").build();
タイプ2:MultipartBody
File file = new File(Environment.getExternalStorageDirectory(), "balabala.mp4");
RequestBody body2 = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("name1", "value1")
//
.addFormDataPart("name2", "filename1", RequestBody.create(MediaType.parse("image/png"), file))
.addPart(Headers.of( "Content-Disposition", "form-data; name=\"name3\""),
RequestBody.create(null, "value2"))
//
.addPart(Headers.of("Content-Disposition", "form-data; name=\"name4\"; filename =\"filename2\""),
RequestBody.create(MediaType.parse("image/png"), file))
.build();
2.Requestの作成
Request request = new Request.Builder()
.url("https://www.baidu.com/")
.post(/* RequestBody */)
.build();
注意事項
RequestBody body1 = new FormBody.Builder().add("username", "name").build();
File file = new File(Environment.getExternalStorageDirectory(), "balabala.mp4");
RequestBody body2 = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("name1", "value1")
//
.addFormDataPart("name2", "filename1", RequestBody.create(MediaType.parse("image/png"), file))
.addPart(Headers.of( "Content-Disposition", "form-data; name=\"name3\""),
RequestBody.create(null, "value2"))
//
.addPart(Headers.of("Content-Disposition", "form-data; name=\"name4\"; filename =\"filename2\""),
RequestBody.create(MediaType.parse("image/png"), file))
.build();
Request request = new Request.Builder()
.url("https://www.baidu.com/")
.post(/* RequestBody */)
.build();
baseUrl
は、/
で終わる必要があります.onResponse()
UIスレッドでは、UIを更新することができる.