2016-10-10 2 views
12

hochladen Ich habe ein Bild von Briefträger wie unten. Wie man das gleiche in Retrofit 2 macht. enter image description hereWie Bilddatei in Retrofit 2

Ich habe die Schnittstelle wie folgt erklärt.

@Multipart 
@POST("/api/Pharmarcy/UploadImage") 
Call<ResponseBody> uploadPrescriptionImage(
     @Query("accessToken") String token, 
     @Query("pharmarcyRequestId") int pharmacyRequestedId, 
     @Part MultipartBody.Part image); 
+0

Also, was ist das Problem? – Piyush

Antwort

25
@Multipart 
@POST("user/updateprofile") 
Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id, @Part("full_name") RequestBody fullName, @Part MultipartBody.Part image, @Part("other") RequestBody other); 

//pass it like this 
File file = new File("/storage/emulated/0/Download/Corrections 6.jpg"); 
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); 

// add another part within the multipart request 
RequestBody fullName = 
        RequestBody.create(
          MediaType.parse("multipart/form-data"), "Your Name"); 

service.updateProfile(id, fullName, body, other) 

Blick auf die Art, wie ich die mehrteiliger und String params

Hoffnung bin vorbei das wird Ihnen helfen!

+0

Funktioniert wie ein Charme –

+0

es funktioniert für mich. –

+0

Können Sie bitte erklären, was ist die "user_id", "full_name" und "andere"? Ist es möglich, Ihre Antwort basierend auf der Frage params zu aktualisieren? –

1

Mit Retrofit 2.0 Sie diese verwenden:

@Multipart 
    @POST("uploadImage") 
    Call<ResponseBody> uploadImage(@Part("file\"; fileName=\"myFile.png\" ")RequestBody requestBodyFile, @Part("image") RequestBody requestBodyJson); 

eine Anfrage Hersteller:

File imgFile = new File("YOUR IMAGE FILE PATH"); 
RequestBody requestBodyFile = RequestBody.create(MediaType.parse("image/*"), imgFile); 
RequestBody requestBodyJson = RequestBody.create(MediaType.parse("text/plain"), 
        retrofitClient.getJsonObject(uploadRequest)); 



//make sync call 
Call<ResponseBody> uploadBundle = uploadImpl.uploadImage(requestBodyFile, requestBodyJson); 
Response<BaseResponse> response = uploadBundle.execute(); 

siehe https://square.github.io/retrofit/

0
@Multipart 
@POST(Config.UPLOAD_IMAGE) 
Observable<Response<String>> uploadPhoto(@Header("Access-Token") String header, @Part MultipartBody.Part imageFile); 

Und Sie können diese api wie folgt aufrufen:

public void uploadImage(File file) { 
    // create multipart 
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile); 

    // upload 
    getViewInteractor().showProfileUploadingProgress(); 

    Observable<Response<String>> observable = api.uploadPhoto("",body); 

    // on Response 
    subscribeForNetwork(observable, new ApiObserver<Response<String>>() { 
     @Override 
     public void onError(Throwable e) { 
      getViewInteractor().hideProfileUploadingProgress(); 
     } 

     @Override 
     public void onResponse(Response<String> response) { 

      if (response.code() != 200) { 
       Timber.d("error " + response.code()); 
       return; 
      } 
      getViewInteractor().hideProfileUploadingProgress(); 
      getViewInteractor().onProfileImageUploadSuccess(response.body()); 

     } 
    }); 

} 
3

Bild hochladen Hier Dieser Link enter image description here

http://mushtaq.16mb.com/retrofit_example/uploads/

import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 

class AppConfig { 

    private static String BASE_URL = "http://mushtaq.16mb.com/"; 

    static Retrofit getRetrofit() { 

     return new Retrofit.Builder() 
       .baseUrl(AppConfig.BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
    } 
} 

======================================================== 
import okhttp3.MultipartBody; 
import okhttp3.RequestBody; 
import retrofit2.Call; 
import retrofit2.http.Multipart; 
import retrofit2.http.POST; 
import retrofit2.http.Part; 

interface ApiConfig { 
    @Multipart 
    @POST("retrofit_example/upload_image.php") 
    Call<ServerResponse> uploadFile(@Part MultipartBody.Part file, 
            @Part("file") RequestBody name); 

    /*@Multipart 
    @POST("ImageUpload") 
    Call<ServerResponseKeshav> uploadFile(@Part MultipartBody.Part file, 
            @Part("file") RequestBody name);*/ 

    @Multipart 
    @POST("retrofit_example/upload_multiple_files.php") 
    Call<ServerResponse> uploadMulFile(@Part MultipartBody.Part file1, 
             @Part MultipartBody.Part file2); 
} 






https://drive.google.com/open?id=0BzBKpZ4nzNzUMnJfaklVVTJkWEk 


    [1]: https://i.stack.imgur.com/K01kZ.png 
+0

Seine Funktionsweise Warum negativ markieren –

+0

kannst du den php side code anzeigen ?? –

0

Es ist ganz einfach Klicken sehen. Hier ist die API-Schnittstelle

public interface Api { 

    @Multipart 
    @POST("upload") 
    Call<MyResponse> uploadImage(@Part("image\"; filename=\"myfile.jpg\" ") RequestBody file, @Part("desc") RequestBody desc); 

} 

Und Sie können den folgenden Code verwenden, um einen Anruf zu tätigen.

private void uploadFile(File file, String desc) { 

     //creating request body for file 
     RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file); 
     RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc); 

     //The gson builder 
     Gson gson = new GsonBuilder() 
       .setLenient() 
       .create(); 


     //creating retrofit object 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(Api.BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create(gson)) 
       .build(); 

     //creating our api 
     Api api = retrofit.create(Api.class); 

     //creating a call and calling the upload image method 
     Call<MyResponse> call = api.uploadImage(requestFile, descBody); 

     //finally performing the call 
     call.enqueue(new Callback<MyResponse>() { 
      @Override 
      public void onResponse(Call<MyResponse> call, Response<MyResponse> response) { 
       if (!response.body().error) { 
        Toast.makeText(getApplicationContext(), "File Uploaded Successfully...", Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show(); 
       } 
      } 

      @Override 
      public void onFailure(Call<MyResponse> call, Throwable t) { 
       Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

Quelle: Retrofit Upload File Tutorial.