0

in my app I have to send a photo to the server and the server will give a trans_id in the response, but I have an 500 internal server error. I tried with postman and everything is okay. My retrofit interface is:

@Headers("Content-Type: application/json")
@Multipart
@POST("photo-mekanik")
Call<PhotoMekanik> getPhotoMekanik(@Part MultipartBody.Part mekanik,
                                   @Part("machineid") RequestBody machineid,
                                   @Header("Authorization") String authorization);

Based on some answer on SO, I tried to add anotation @FormUrlEncoded but, Multipart and @FormUrlEncoded cannot be used together. In my retriever I have:

public void sendPhotoMekanik(Callback<PhotoMekanik> photoMekanikCallback, MultipartBody.Part photoMekanik, RequestBody machineid, String authorization) {
        Call<PhotoMekanik> photoMekanikCall = service.getPhotoMekanik(photoMekanik, machineid, authorization);
        photoMekanikCall.enqueue(photoMekanikCallback);

and my retrofit callback is:

private void  UploadPhotoMekanik() {
        SharedPreferences editor = getSharedPreferences(MY_PREFERENCE, MODE_PRIVATE);
        String token = editor.getString("token", "");

        Intent intent = getIntent();
        machineid = Objects.requireNonNull(intent.getExtras()).getString("machineId");

        RequestBody fileImage = RequestBody.create(file, MediaType.parse("image/png"));

        MultipartBody.Part mImagePart = MultipartBody.Part.createFormData("mekanik", file.getName(), fileImage);
        PostRetrivers uploadPhotoMekanik = new PostRetrivers();
        Callback<PhotoMekanik> callback = new Callback<PhotoMekanik>() {
            @Override
            public void onResponse(Call<PhotoMekanik> call, Response<PhotoMekanik> response) {
                System.out.println();
            }

            @Override
            public void onFailure(Call<PhotoMekanik> call, Throwable t) {
                Toast.makeText(PutWeeklyDatasActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        RequestBody mekID = RequestBody.create(machineid, MediaType.parse("text/plain"));

        uploadPhotoMekanik.sendPhotoMekanik(callback, mImagePart, mekID, token);
    }

Edit: I solved this issue replacing: @Headers("Accept: application/json")

but now I have another error:

expected begin_object but was number at line 1 column 6 path $ 

enter image description here

That is the request when I use postman, it gives me the trans_id which is only a number and doesn't contain any brackets. I don't understand error, because I'm new to retrofit. My model:

public class PhotoMekanik implements Serializable {
    @SerializedName("machineid")
    private String machineid;
    @SerializedName("mekanik")
    private String mekanik;
    @SerializedName("trans_id")
    private String trans_id;

    public PhotoMekanik(String machineid, String mekanik, String trans_id) {
        this.machineid = machineid;
        this.mekanik = mekanik;
        this.trans_id = trans_id;
    }

    public String getMachineid() {
        return machineid;
    }

    public void setMachineid(String machineid) {
        this.machineid = machineid;
    }

    public String getMekanik() {
        return mekanik;
    }

    public void setMekanik(String mekanik) {
        this.mekanik = mekanik;
    }

    public String getTrans_id() {
        return trans_id;
    }

    public void setTrans_id(String trans_id) {
        this.trans_id = trans_id;
    }
}
Erald Developer
  • 359
  • 1
  • 6
  • 16

0 Answers0