最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Retrofit 2.0框架上傳圖片解決方案

 更新時(shí)間:2016年03月03日 09:49:53   投稿:lijiao  
這篇文章主要介紹了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,以此類(lèi)推。

大家看到上面覺(jué)得寫(xiě)法很漏,但是用于能力有限,只能想到這樣。用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); 

這樣寫(xiě)看上去很是高端,不幸的是只能傳一張

3.最后是實(shí)現(xiàn)胡過(guò)程
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對(duì)象

 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)于A(yíng)ndroid上傳功能的更多內(nèi)容請(qǐng)點(diǎn)擊專(zhuān)題:Android上傳操作匯總進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

定安县| 聂拉木县| 瓦房店市| 陇川县| 名山县| 双峰县| 胶州市| 栾城县| 自贡市| 平谷区| 海宁市| 蒲城县| 新河县| 浙江省| 贵德县| 巴林左旗| 湟中县| 右玉县| 桦南县| 旌德县| 彭泽县| 高密市| 兴国县| 葫芦岛市| 鹤峰县| 丹巴县| 当雄县| 西昌市| 南丰县| 新民市| 石屏县| 安徽省| 西乌珠穆沁旗| 易门县| 绵竹市| 克山县| 拉萨市| 泰宁县| 庄浪县| 界首市| 晴隆县|