Android Retrofit 2.0框架上傳圖片解決方案
本文為大家分享了 Android Retrofit 2.0框架上傳圖片解決方案,具體內(nèi)容如下
1.單張圖片的上傳
/**
* 上傳一張圖片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);
2.多張圖片的上傳
/**
* 上傳三張圖片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs,
@Part("file\"; filename=\"image.png\"")RequestBody imgs1,
@Part("file\"; filename=\"image.png\"")RequestBody imgs3);
注意:目前是提供傳3張,要想多上傳目前我發(fā)現(xiàn)的方法就是想要多傳一張,就多增加一個(gè)參數(shù)
@Part("file\"; filename=\"image.png\"")RequestBody imgs,以此類推。
大家看到上面覺得寫法很漏,但是用于能力有限,只能想到這樣。用Java中的可變參數(shù)解決之后,就只能傳一張。不能多張。
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody ...imgs);
調(diào)用:
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);
這樣寫看上去很是高端,不幸的是只能傳一張
3.最后是實(shí)現(xiàn)胡過程
3.1創(chuàng)建FileUploadService接口
public interface FileUploadService {
/**
* 上傳一張圖片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);
/**
* 上傳三張圖片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs,
@Part("file\"; filename=\"image.png\"")RequestBody imgs1,
@Part("file\"; filename=\"image.png\"")RequestBody imgs3);
}
3.2創(chuàng)建Retrofit對象
private static final Retrofit sRetrofit = new Retrofit .Builder() .baseUrl(ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) // .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作為回調(diào)適配器 .build(); private static final FileUploadService apiManager = sRetrofit.create(FileUploadService.class);
3.3調(diào)用上傳的方法
public static void upload(String path){
String descriptionString = "hello, this is description speaking";
String[] m = new String[2];
m[0]= "share.png";
m[1]= "Screenshot_20160128-140709.png";
File[] ssssss= new File[2];
File file1 = new File("/storage/emulated/0/sc/share.png");
File file = new File("/storage/emulated/0/Pictures/ScreenShots/Screenshot_20160128-140709.png");
ssssss[0]=file;
ssssss[0]=file1;
RequestBody requestBody[] = new RequestBody[3];
RequestBody requestBody1 =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
RequestBody requestBody2 =
RequestBody.create(MediaType.parse("multipart/form-data"), file1);
requestBody[0]=requestBody1;
requestBody[1]=requestBody2;
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
Log.v("Upload", response.message());
Log.v("Upload", "success");
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.toString());
}
});
}
4.服務(wù)器段代碼:
服務(wù)器用的是struts接收:
@Controller
public class GetToken extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File[] file;
private String[] fileName;
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileName() {
return fileName;
}
public void setFileName(String[] fileName) {
this.fileName = fileName;
}
@Action("/upload")
public void login() {
System.out.println("------"+Arrays.toString(file));
System.out.println("------"+Arrays.toString(fileName));
}
}
關(guān)于Android上傳功能的更多內(nèi)容請點(diǎn)擊專題:Android上傳操作匯總進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android實(shí)現(xiàn)上傳圖片至java服務(wù)器
- android上傳圖片到PHP的過程詳解
- Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug
- Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
- Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- Android異步上傳圖片到PHP服務(wù)器
- Android使用OkHttp上傳圖片的實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫
相關(guān)文章
android中Fragment+RadioButton實(shí)現(xiàn)底部導(dǎo)航欄
本篇文章主要介紹了android中Fragment+RadioButton實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能
這篇文章主要介紹了Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)
這篇文章主要介紹了android 開發(fā)隱藏標(biāo)題欄的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng)
滑動(dòng)刪除的部分主要包含兩個(gè)部分, 一個(gè)是內(nèi)容區(qū)域(用于放置正常顯示的view),另一個(gè)是操作區(qū)域(用于放置刪除按鈕)。下面通過本文給大家介紹Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng),需要的朋友可以參考下2017-06-06
Android利用ConstraintLayout實(shí)現(xiàn)漂亮的動(dòng)畫詳解
最近在無意中看到一篇關(guān)于ConstraintLayout的文章,ConstraintLayout是Android Studio 2.2中主要的新增功能之一,下面這篇文章主要給大家介紹了關(guān)于Android利用ConstraintLayout實(shí)現(xiàn)漂亮的動(dòng)畫的相關(guān)資料,需要的朋友可以參考下。2017-05-05
Android實(shí)現(xiàn)RecyclerView下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)RecyclerView下拉刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
解決android viewmodel 數(shù)據(jù)刷新異常的問題
這篇文章主要介紹了解決android viewmodel 數(shù)據(jù)刷新異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

