2017-08-01 37 views
0

我發送請求這樣我有一個需要發送到服務器的3個參數(圖像,ID,類型)的後調用,我如何發送改造。即時通訊使用的多,但它不會讓我在

public interface iUpload{ 
    @Multipart 
    @POST("/uploadmultiplepropimages") 
    SamplePojoClass getUploadData(
      @Part("prop_id") MultipartBody.Part prop_id, 
      @Part("type") MultipartBody.Part type, 
      @Part("prop_photos") MultipartBody.Part prop_photos 
    ); 
} 

這裏是我的聲明並調用響應方法

RequestBody lRidd = RequestBody.create(MediaType.parse("text/plain"), "10000001"); 
     RequestBody lRtype = 
RequestBody.create(MediaType.parse("text/plain"), "single"); 

     RequestBody lRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), photoFile); 
     MultipartBody.Part lFile = MultipartBody.Part.createFormData("prop_photos", photoFile.getName(), lRequestBody); 
     MultipartBody.Part idd = MultipartBody.Part.createFormData("prop_id", "10000001"); 
     MultipartBody.Part type = MultipartBody.Part.createFormData("type", "single"); 
uploadImageResponse = RequestResponse.getUploadData(idd,type,lFile); 

`

回答

0

使用此代碼:

private final WebApi api = new Retrofit.Builder() 
       .baseUrl(Constants.BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build().create(WebApi.class); 





public void createGoal(String goalTitle, String goalDueDate, String goalComment, String goalCategory, String goalReminderDate, String account, File image, String userId, final CreateGoalHandler handler) { 

     if (image != null) { 
      RequestBody requestBodies = RequestBody.create(MediaType.parse("multipart/form-data"), image); 
      part = MultipartBody.Part.createFormData("profileImage", image.getName(), requestBodies); 
     } 

     RequestBody goalTtle = RequestBody.create(MediaType.parse("multipart/form-data"), goalTitle); 
     RequestBody goalDueDte = RequestBody.create(MediaType.parse("multipart/form-data"), goalDueDate); 
     RequestBody goalCmnt = RequestBody.create(MediaType.parse("multipart/form-data"), goalComment); 
     RequestBody goalCtgry = RequestBody.create(MediaType.parse("multipart/form-data"), goalCategory); 
     RequestBody goalReminderDtes = RequestBody.create(MediaType.parse("multipart/form-data"), goalReminderDate); 
     RequestBody accnt = RequestBody.create(MediaType.parse("multipart/form-data"), account); 
     RequestBody userid = RequestBody.create(MediaType.parse("multipart/form-data"), userId); 


     Call<CreateGoalPrp> callback = api.createGoal(goalTtle, goalDueDte, goalCmnt, goalCtgry, goalReminderDtes, accnt, part, userid); 
     callback.enqueue(new Callback<CreateGoalPrp>() { 
      @Override 
      public void onResponse(Call<CreateGoalPrp> call, Response<CreateGoalPrp> response) { 
       handler.OnSuccess(response.body()); 
      } 

      @Override 
      public void onFailure(Call<CreateGoalPrp> call, Throwable t) { 
       handler.OnSuccess(null); 
      } 
     }); 
    } 

//////////////////////////// //

public interface WebApi { 

    @Multipart 
    @POST("createGoal.php") 
    Call<CreateGoalPrp> createGoal(@Part("goalTitle") RequestBody goalTitle, @Part("goalDueDate") RequestBody goalDueDate, @Part("goalComment") RequestBody goalComment, @Part("goalCategory") RequestBody goalCategory, @Part("goalReminderDate") RequestBody goalReminderDate, @Part("account") RequestBody account, @Part MultipartBody.Part profileImage, @Part("userId") RequestBody userId); 
0

使用此界面中的任何所謂EmployeeAPI

@Multipart 
@POST("/Home/UploadProfilePic") 
void UploadFile(@Part("file")TypedFile file, @Part("StaffIDNO")String StaffIDNO, Callback<Response> callback); 

功能需要兩個參數文件和字符串。

呼叫使用下面的函數

public void UploadFile(final TypedFile file, final String StaffId) { 

     RestAdapter.Builder builder = new RestAdapter.Builder(); 
     final GlobalRecordFetch globalRecordFetch = new GlobalRecordFetch(getActivity()); 
     builder.setEndpoint(globalRecordFetch.ROOT_URL); 
     builder.setLogLevel(RestAdapter.LogLevel.FULL); 
     RestAdapter restAdapter = builder.build(); 
     EmployeeAPI api = restAdapter.create(EmployeeAPI.class); 
     api.UploadFile(
       file, 
       StaffId, 
       new Callback<Response>() { 
        @Override 
        public void success(Response result, Response response) { 
         BufferedReader reader = null; 
         String output = ""; 
         try { 
          reader = new BufferedReader(new InputStreamReader(result.getBody().in())); 
          output = reader.readLine(); 

         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         loading.dismiss(); 
        } 
        @Override 
        public void failure(RetrofitError error) { 

        } 
       } 
     ); 


    } 
在活動的方法
相關問題