??????? 現(xiàn)在市面上比較火熱的Android網(wǎng)絡(luò)框架,就我知道的沙廉,volley拘荡,okhttp,retrofit+rxjava撬陵,當(dāng)然rxjava不算是網(wǎng)絡(luò)框架珊皿,只是配合retofit使用更為方便网缝,volley是我使用得比較多的一個(gè)網(wǎng)絡(luò)框架,但是用他來(lái)做輕量的數(shù)據(jù)請(qǐng)求自是沒(méi)問(wèn)題蟋定,但是在下載和上傳文件上粉臊,似乎就會(huì)暴露一些問(wèn)題,啥問(wèn)題我也不知道驶兜,因?yàn)槲抑皇遣锁B(niǎo)扼仲,這些東西也是一個(gè)自己的理解,如果有錯(cuò)誤的地方抄淑,勿噴屠凶。
???????? 下面就進(jìn)入正題,本來(lái)我是想使用retrofit肆资,但是我又想一口吃成胖子矗愧,對(duì)于rxjava的學(xué)習(xí)上堵住,所以轉(zhuǎn)戰(zhàn)okhttp郑原,以下則是我對(duì)okhttp的一些理解和使用:
????????? 因?yàn)槲矣玫氖茿ndroid Studio贱枣,所以首先依賴
compile'com.squareup.okhttp3:okhttp:3.4.2'
? ,依賴完以后就可以使用了颤专,首先:
private OkHttpClient okHttpClient;
private Request request;
private Call call;
因?yàn)椴还苁莋et請(qǐng)求,post請(qǐng)求钠乏,還是上傳下載文件栖秕,都需要這三個(gè),總不能一個(gè)文件就新建一個(gè)對(duì)象吧晓避,然后我在構(gòu)造函數(shù)中實(shí)例化了
public Http OkHttp() {
okHttpClient=new OkHttpClient();
}
簇捍,get請(qǐng)求如下:
public Call getOkhttp() {
???????????? request=new Request.Builder()
??????????????????????????????????????? ? ? ? ? ? ? ? .url(url)
??????????????????????????? ? ? ? ? ? ? ? ??????????? .build();
????????????? call=okHttpClient.newCall(request);
????????????? return call;
}
我是把call直接返回出來(lái),這樣我用著舒服些俏拱, 正規(guī)寫(xiě)法如下:
public void getOkhttp() {
request=newRequest.Builder()
.url(url)
.build();
call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call,IOException e) {
//這里面試請(qǐng)求錯(cuò)誤調(diào)用
}
@Override
public void onResponse(Call call,Response response) throws IOException{
//這是是請(qǐng)求成功返回的
//得到返回信息則是 response.body().string()
}
});
}
暑塑,post請(qǐng)求如下:
public Call postOkhttp(RequestBody requestBody) {
request=new Request.Builder()
.url(url)
.post(requestBody)
.build();
call=okHttpClient.newCall(request);
return call;
}
鍵值對(duì)我是將其提取出來(lái),定義如下:
RequestBody requestBody=new FormBody.Builder()
.add("key","valuse")
.build();
add方法則是往里面加入請(qǐng)求體锅必,使用還是很簡(jiǎn)單的事格,
上傳文件如下:
public Call FileOkhttp(String filename,String key) {
File file=new File(Environment.getExternalStorageDirectory(), filename);
RequestBody fileBody=RequestBody.create(MediaType.parse("application/octet-stream"),file);
RequestBody requestBody=new MultipartBody.Builder()
?????????????????????????????????????????????????????????????????????????????????????? .setType(MultipartBody.FORM)
?????????????????????????????????????????????????????????????????????????????????????? .addPart(Headers.of("Content-Disposition","form-data; name=\"username\""),
??????????????????????????????????????????????????????????????????????????????????????? RequestBody.create(null, key))//上傳文件名
??????????????????????????????????????????????????????????????????????????????????????? .addPart(Headers.of("Content-Disposition","form-data; name=\"mFile\"; filename =\"wjd.mp4\""),fileBody)
???????????????????????????????????????????????????????????????????????????????????????? .build();
request=new Request.Builder()
???????????????????????????????????????? .url(url)
????????????????????????????????????????? .post(requestBody)
?????????????????????????????????????????? .build();
call=okHttpClient.newCall(request);
return call;
}