欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年03月03日 09:49:53   投稿:lijiao  
這篇文章主要介紹了Android Retrofit 2.0框架上傳一張與多張圖片解決方案,感興趣的小伙伴們可以參考一下

本文為大家分享了 Android Retrofit 2.0框架上傳圖片解決方案,具體內容如下

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)的方法就是想要多傳一張,就多增加一個參數(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); 

 調用:
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null); 

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

3.最后是實現(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作為回調適配器
 .build();

 private static final FileUploadService apiManager = sRetrofit.create(FileUploadService.class);

 3.3調用上傳的方法

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.服務器段代碼:
服務器用的是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)); 
 } 
 
 
 
} 

關于Android上傳功能的更多內容請點擊專題:Android上傳操作匯總進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助。

相關文章

最新評論