1.寫在前面的話
首先說明,我還沒太搞懂retrofit胸哥,目前涯竟,這篇博客只能給出這幾個(gè)內(nèi)容。
- 文件上傳
- 文件下載
- 文件下載的進(jìn)度監(jiān)聽
還有這兩點(diǎn)沒弄好空厌,
- 多文件一次上傳 (批量上傳)
- 文件上傳進(jìn)度監(jiān)聽
當(dāng)前使用版本
compile 'com.squareup.retrofit2:retrofit:2.0.2'
2. 文件上傳
2.1 api 接口編寫
public interface uploadfileApi {
@Multipart
@POST("/fileabout.php")
Call<String> upload(@Part("fileName") String des,
@Part("file\"; filename=\"1.txt") RequestBody file);
}
- @Part("fileDes") String des 可以加一些描述信息(可以不加)
- @Part("file"; filename="1.txt") 格式不變庐船,只需將1.text 對(duì)應(yīng)的替換為你想在服務(wù)器生成的文件名稱
- 如果想傳多個(gè)文件,多次請(qǐng)求嘲更,當(dāng)然筐钟,也可以像表單一樣(還沒弄好)
當(dāng)然,上面這種辦法的靈活性差了點(diǎn)赋朦,我們可以選擇下面這種寫法
public interface uploadfileApi {
@Multipart
@POST("/fileabout.php")
Call<String> upload_2(@PartMap Map<String,RequestBody> params);
}
2.2 上傳文件
第一種api接口對(duì)應(yīng)的代碼
Retrofit retrofit= new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://192.168.56.1")
.build();
uploadfileApi service =retrofit.create(uploadfileApi.class);
File file = new File(Environment.getExternalStorageDirectory() + "/" + "1.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
Call<String> model = service.upload("this is txt",requestBody);
model.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.e(TAG, "onResponse: " + response.body().toString() );
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
- baseurl 為你的服務(wù)器地址,(我這里在局域網(wǎng))
- file 文件為你手機(jī)中某個(gè)存在的文件
第二中API篓冲,我們只需要將相應(yīng)第一種中的參數(shù)用map存起來,不多說了宠哄。
2.3 服務(wù)器接受文件
服務(wù)器接受文件的代碼就簡(jiǎn)單多了壹将,我這里以php為例
<?php
//var_dump($_POST);
//var_dump($_FILES);
$myfile = fopen("testfile.txt", "w");
fwrite($myfile, $_FILES["file"]["tmp_name"]."\n"
."D:\WWW"."\".$_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], "D:\WWW"."\".$_FILES["file"]["name"]);
> 上面這個(gè)代碼就是將文件的文件名寫入到textfile.txt文件中,并且將文件寫在當(dāng)前d:\www\目錄下毛嫉,文件名就是上傳的文件名诽俯。
結(jié)果如下如:
![這里寫圖片描述](http://upload-images.jianshu.io/upload_images/1622644-a07ca9595c31c20c?imageMogr2/auto-orient/strip)
### 3. 文件下載及速度監(jiān)聽
Retrofit并沒有給我們提供文件下載進(jìn)度的相關(guān)信息,但是承粤,我們還是可以從一些渠道知道如何監(jiān)聽下載進(jìn)度暴区,在OKHTTP的官方demo里面有一個(gè)Progress.java的文件,從名字上就知道與進(jìn)度有關(guān)辛臊。[github地址](https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java)
#### 3.1 改造改造ResponseBody
okhttp3默認(rèn)的responsebody是不能滿足我們的要求的仙粱,(不能知道進(jìn)度的相關(guān)信息),我們需要作出改造浪讳,首先需要個(gè)接口缰盏,監(jiān)聽進(jìn)度信息。其次淹遵,好吧口猜,我承認(rèn)這是廢話,我們只需要把Progress.java中我們需要的拿出來就好透揣。
##### 3.1.1 interface
public interface ProgressListener {
/**
* @param progress 已經(jīng)下載或上傳字節(jié)數(shù)
* @param total 總字節(jié)數(shù)
* @param done 是否完成
*/
void onProgress(long progress, long total, boolean done);
}
##### 3.1.2 ProgressResponseBody
public class ProgressResponseBody extends ResponseBody {
private final ResponseBody responseBody;
private final ProgressListener listener;
private BufferedSource bufferedSource;
public ProgressResponseBody(ResponseBody responseBody,ProgressListener listener){
this.responseBody = responseBody;
this.listener = listener;
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (null == bufferedSource){
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long totalBytesRead = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
listener.onProgress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
return bytesRead;
}
};
}
}
恩济炎,就是這些東西,別為我okio的相關(guān)知識(shí)辐真,我也正在學(xué)呢须尚。這個(gè)文件就是ophttp3的官方demo里面的東西崖堤。
#### 3.2 使用自己的okhttpclient
我們需要通過OkHttpClient的攔截器去攔截Response,并將我們的ProgressReponseBody設(shè)置進(jìn)去耐床,這樣才能監(jiān)聽進(jìn)度密幔。那么,我們?cè)趺粗vclient設(shè)置進(jìn)去呢撩轰。通過觀察Retrofit的結(jié)構(gòu)發(fā)現(xiàn)胯甩,Builder下面有client()方法可以設(shè)置,好堪嫂,那么我們通過Retrofit.Builder來創(chuàng)建(這樣我們可以配置了)偎箫。
![這里寫圖片描述](http://upload-images.jianshu.io/upload_images/1622644-550943bdced92e72?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
相關(guān)代碼如下
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://192.168.56.1");
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response orginalResponse = chain.proceed(chain.request());
return orginalResponse.newBuilder()
.body(new ProgressResponseBody(orginalResponse.body(), new ProgressListener() {
@Override
public void onProgress(long progress, long total, boolean done) {
Log.e(TAG, Looper.myLooper()+"");
Log.e(TAG, "onProgress: " + "total ---->" + total + "done ---->" + progress );
}
}))
.build();
}
})
.build();
DownLoadApi api = builder.client(client)
.build().create(DownLoadApi.class);
** 注意進(jìn)度的監(jiān)聽發(fā)生在子線程中,要切記**
#### 3.3 將response寫入到文件里
寫入的操作就簡(jiǎn)單了皆串,代碼如下淹办,沒什么好說的。
Call<ResponseBody> call = api.getFile("image_text.png");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
InputStream is = response.body().byteStream();
File file = new File(Environment.getExternalStorageDirectory(), "text_img.png");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
fos.flush();
}
fos.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG,"success");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
** 注意image_text.png是我事先將這張圖片放入到相應(yīng)路徑下面的恶复,如圖怜森,要確定能訪問到才行 **
![這里寫圖片描述](http://upload-images.jianshu.io/upload_images/1622644-1ca99b3b49abcb4f?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#### 3.4 最后結(jié)果展示
![這里寫圖片描述](http://upload-images.jianshu.io/upload_images/1622644-1103ecc834be2c8f?imageMogr2/auto-orient/strip)
### 4. 總結(jié)
retrofit的功能強(qiáng)大,靈活性強(qiáng)寂玲,但是這就意味著使用起來稍微麻煩一點(diǎn)(至少我是這樣認(rèn)為的)塔插,但是,retrofit依賴于okhttp拓哟,okhttp是有demo供我們學(xué)習(xí)的想许,so,學(xué)習(xí)demo去吧断序,[鏈接地址](https://github.com/square/okhttp/tree/master/samples)