2016-11-24 43 views
1

我是新的改造我想用不同的參數,如名稱,dob,手機上傳單個圖像。我不知道我在哪裏錯了請指導我。我按照這個LINKRetrofit 2.0.2圖像上傳使用多部分

這裏是我的代碼

接口

@Multipart 
    @POST("signup") 
    Call<ResponseBody> getSignup(@Part("name") RequestBody name, @Part("email") RequestBody email, @Part("dob") RequestBody dob, @Part("phone") RequestBody phone, @Part("IMEI") RequestBody IMEI, @Part MultipartBody.Part file); 

上傳代碼

// create RequestBody instance from file 
       RequestBody requestFile = 
         RequestBody.create(MediaType.parse("multipart/form-data"), file); 

       // MultipartBody.Part is used to send also the actual file name 
       MultipartBody.Part body = 
         MultipartBody.Part.createFormData("image", file.getName(), requestFile); 

       RequestBody name = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), et_name.getText().toString()); 

       RequestBody email = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), et_email.getText().toString()); 

       RequestBody dob = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), et_dob.getText().toString()); 

       RequestBody mobile = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), et_mobile.getText().toString()); 


       RequestBody imei = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), IMEI); 

       Call<ResponseBody> responseBodyCall = apiInterface.getSignup(name, email, dob, mobile, imei, body); 
       responseBodyCall.enqueue(new Callback<ResponseBody>() { 
        @Override 
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 

         String response_two = response.body().toString(); 

         Log.i(TAG, "onResponse: " + response_two); 
//      startActivity(new Intent(this, OTPActivity.class)); 
        } 

        @Override 
        public void onFailure(Call<ResponseBody> call, Throwable t) { 
         Log.e("Upload error:", t.getMessage()); 
        } 
       }); 
+0

您有什麼問題interface

@Multipart @POST("edit_profile") Call<TokenResponse> getTokenAccess(@PartMap Map<String, RequestBody> map); 
  • 打電話? – Nithinlal

  • +0

    我不想上傳多個文件只有一個文件我想發送。 –

    +0

    我無法上傳與移動,名稱,imei等不同參數的文件。 –

    回答

    0

    您需要添加相同類型的圖像也看這裏它如何工作, 這也適用於多個圖像。

    @Multipart 
    @POST(ApiConstants.EDIT_REQUEST) 
    Observable<CommonModel> editRequest(@Part("requestid") RequestBody requestid, @PartMap() Map<String, RequestBody> mapFileAndName); 
    
    
    RequestBody requestid1 = RequestBody.create(MediaType.parse("text/plain"), requestid); 
    
    Map<String, RequestBody> files = new HashMap<>(); 
        for (int i = 0; i < mList.size(); i++) { 
         String key = "package_image[" + String.valueOf(i) + "]"; 
         files.put("" + key + "\"; filename=\"" + key + ".png", getRequestFile(new File(mList.get(i).getImagePath()))); 
    } 
    
    
    
    private RequestBody getRequestFile(File file) { 
    
        RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file); 
        return fbody; 
    
    } 
    

    瞭解更多詳情,您可以訪問here

    0

    其帖子的例子多部分

    @Multipart 
    @POST("/api/register?platform=android") 
    Call<Register> register(@Part("photo\"; filename=\"photoName.png\" ") RequestBody photo, 
                @Part("name") RequestBody name, 
                @Part("phoneNumber") RequestBody phoneNumber, 
                @Part("password") RequestBody password,carColor, 
                @Query("language") String language); 
    
    +0

    查詢不適用於翻新2.0.2,只有我們可以發送零件類型 – Saveen

    +0

    我有 - 編譯'com.squareup.retrofit:翻新:2.0.0 -beta2'是不同的? –

    +0

    是的不一樣 – Saveen

    0

    改造2.0開始嘗試這個答案

    @Multipart 
    @POST("/service/users/add_user") 
    Call<AddUsersModel> addUser(@Part("user_name") RequestBody userName, @Part("user_last_name") RequestBody lastName, @Part("user_password") RequestBody password, 
               @Part("user_email") RequestBody email, @Part("user_mobile") RequestBody mobile, @Part MultipartBody.Part user_profile); 
    

    文件路徑MulitpartBody.Part皈依

    MultipartBody.Part profileImageBody = null; 
         RequestBody reqFile = null; 
         try { 
          String filePath = CameraIntentUtil.getFIlePath(); 
          Log.d(TAG, "createAccount: filePath:" + filePath); 
          if (filePath != null) { 
           File file = new File(filePath); 
    
           reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 
           // "user_profile" is my post request key 
           profileImageBody = MultipartBody.Part.createFormData("user_profile", file.getName(), reqFile); 
          } 
         } catch (Exception e) { 
          Log.e(TAG, "createAccount: ", e); 
         } 
    

    字符串RequestBody皈依

    public static RequestBody stringToRequestBody(String data) { 
        return RequestBody.create(MediaType.parse("multipart/form-data"), data); 
    } 
    

    很晚了答案。但我希望它幫ATLEAST人

    0

    測試在改造2.3 .0

    1. 創建您Activity

         Retrofit retrofit=new Retrofit.Builder() 
            .baseUrl(getString(R.string.api_coolpool)) 
            .addConverterFactory(GsonConverterFactory.create()) 
            .build(); 
            File file = new File("/storage/sdcard0/Pictures/OGQ/Puskinn 
            Sharma_Jump roof skyscraper_YkRiRWpYcw.jpg"); 
            String convert_File_2String= String.valueOf(file); 
            String fileNAme=convert_File_2String.substring(convert_File_2String.lastIndexOf("/")+1); 
      RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file); 
      RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "Sunil"); 
      RequestBody id = RequestBody.create(MediaType.parse("text/plain"), "56"); 
      RequestBody lastname= RequestBody.create(MediaType.parse("text/plain"), "Kumar"); 
      Map<String, RequestBody> map = new HashMap<>(); 
      map.put("profile_pic\"; filename=\""+fileNAme+"\" ", fbody); 
      map.put("firstname", name); 
      map.put("user_id", id); 
      map.put("lastname",lastname); 
      
      Call<TokenResponse> tokenResponseCall=service.getTokenAccess(map); 
      tokenResponseCall.enqueue(new Callback<TokenResponse>() { 
          @Override 
          public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) { 
           if (response.isSuccessful()) 
           Log.e("Success", new Gson().toJson(response.body())); 
          else 
           Log.e("unSuccess", new Gson().toJson(response.errorBody())); 
      } 
      
           } catch (Exception e) { 
            e.printStackTrace(); 
           } 
          } 
      
          @Override 
          public void onFailure(Call<TokenResponse> call, Throwable throwable) { 
           Log.e("172","><<>>"+throwable); 
      
          } 
      });