2017-02-08 4 views
0

Я и моя команда пытаются использовать модификацию. Я хочу загрузить изображение с некоторым строковым значением, используя запрос POST. я продолжаю получать eror "filename" не определено. это правильно ?Модернизация, запрос POST для загрузки изображения и строкового значения

private void uploadMultipart(File file) { 
    final String kodedokter = "D021422"; 
    final String bulan = "12"; 
    final String tahun = "2016"; 
    RequestBody photoBody = RequestBody.create(MediaType.parse("image/*"), file); 
    MultipartBody.Part photoPart = MultipartBody.Part.createFormData("sign", 
      file.getName(), photoBody); 
    RequestBody kodedokter1 = RequestBody.create(MediaType.parse("kode_dokter"), kodedokter); 
    RequestBody bulan1 = RequestBody.create(MediaType.parse("bulan"), bulan); 
    RequestBody tahun1 = RequestBody.create(MediaType.parse("tahun"), tahun); 
    uploadService = new UploadService(); 
    uploadService.uploadPhotoMultipart(kodedokter1,bulan1,tahun1, photoPart, new Callback() { 
     @Override 
     public void onResponse(Call call, Response response) { 
      BaseResponse baseResponse = (BaseResponse) response.body(); 

      if(baseResponse != null) { 
       Toast.makeText(MainActivity.this, baseResponse.getMessage(), Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call call, Throwable t) { 
      t.printStackTrace(); 
     } 
    }); 
} 

ответ

0

Проверить ниже код

private void sendSinglePhoto(String location, String longitude, String latitude, File singleImage) { 
    Constant.showProgressDialog(SinglePhotoActivity.this); 

    RequestBody Location = RequestBody.create(MediaType.parse("multipart/form-data"), location); 
    RequestBody Longitude = RequestBody.create(MediaType.parse("multipart/form-data"), longitude); 
    RequestBody Latitude = RequestBody.create(MediaType.parse("multipart/form-data"), latitude); 

    RequestBody ProfileImage; 
    MultipartBody.Part body; 
    if (singleImage != null) { 
     ProfileImage = RequestBody.create(MediaType.parse("image/*"), singleImage); 
     body = MultipartBody.Part.createFormData("single_image", singleImageFile.getName(), ProfileImage); 
    } else { 
     ProfileImage = RequestBody.create(MediaType.parse("multipart/form-data"), ""); 
     body = MultipartBody.Part.createFormData("single_image", "", ProfileImage); 
    } 

    uploadSinglePhoto(this, Location, Longitude, Latitude, body); 
} 


public void uploadSinglePhoto(final NetworkResponseListener listener, RequestBody location, RequestBody longitude, RequestBody latitude, MultipartBody.Part singleImage) { 

    final Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(RestClient.ROOT) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    RestClient.NetworkCall networkCall = retrofit.create(RestClient.NetworkCall.class); 


    Call<UploadPhotoResponse> response = networkCall.uploadSinglePhoto(Prefs.getAuth(App.getContext()), location, longitude, latitude, singleImage); 

    response.enqueue(new Callback<UploadPhotoResponse>() { 
     @Override 
     public void onResponse(Call<UploadPhotoResponse> call, Response<UploadPhotoResponse> response) { 
      if (response.code() == Constant.SUCCESS_STATUS) { 
       "Your Success Code should be here" 
      } else { 
       "Your Success Code should be here" 
      } 
    } 
     @Override 
     public void onFailure(Call<UploadPhotoResponse> call, Throwable t) { 

     } 
    }); 
} 


    @Multipart 
    @POST("your URL") 
    Call<UploadPhotoResponse> uploadSinglePhoto(@Header("Authorization") String auth, @Part("location") RequestBody location, @Part("longitude") RequestBody longitude, @Part("latitude") RequestBody latitude, @Part MultipartBody.Part singleImage); 
+0

чем смысл "single_image" в этом блоке 'если (SingleImage! = NULL) { ProfileImage = RequestBody.create (MediaType.parse (" изображение/* "), singleImage); body = MultipartBody.Part.createFormData ("single_image", singleImageFile.getName(), ProfileImage); } else { ProfileImage = RequestBody.create (MediaType.parse ("multipart/form-data"), ""); body = MultipartBody.Part.createFormData ("single_image", "", ProfileImage); } ' –

+0

" single_image "- это имя параметра – Akash

Смежные вопросы