Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't upload image file using PartMap #3140

Open
zombozo12 opened this issue Jul 2, 2019 · 0 comments
Open

Can't upload image file using PartMap #3140

zombozo12 opened this issue Jul 2, 2019 · 0 comments

Comments

@zombozo12
Copy link

zombozo12 commented Jul 2, 2019

First time using retrofit2, and immediately making a not-too-small app. I'm kinda rushed learning by doing..

I was trying to upload an image with PartMap and RequestBody, but the image won't uploaded. The request body (text, etc) works perfectly. I've trying to find missing parameters or typos but I didn't found anything wrong. This code run perfectly but the problem is, it cannot upload the image file. What did I do wrong in here?

public interface ClientRoute {    
 @Multipart
 @POST("attendance/in")
 Call < DataAttendance > inAttendance(
  @PartMap Map < String, RequestBody > partMap,
  @HeaderMap Map < String, String > headers);
}

My guess is TimeInterface anonymous class cannot pass photoPath into its methods. But im not sure :/ Because other variables passed.
Or maybe, file cannot be proccessed by RequestBody.

public void inAttendance(int userId, String token, int storeId, String timeAttendance, String photoPath, int refId,
 double latitude, double longitude) {

 timeController.getTimeaAsync("yyyy-MM-dd HH:mm:ss", false, new TimeInterface() {
  @Override
  public void onSuccess(@NonNull String value) {

   Map < String, RequestBody > params = new HashMap < > ();
   params.put("storeId", requestBody(String.valueOf(storeId)));
   params.put("time_attendance", requestBody(value));
   params.put("refId", requestBody(String.valueOf(refId)));
   params.put("photo", RequestBody.create(MediaType.parse("image/*"), photoPath));
   params.put("latitude", requestBody(String.valueOf(latitude)));
   params.put("longitude", requestBody(String.valueOf(longitude)));

   Map < String, String > headers = new HashMap < > ();
   headers.put("userId", String.valueOf(userId));
   headers.put("token", token);

   clientRoute = ClientUtils.getClientRoute();
   clientRoute.inAttendance(params, headers).enqueue(new Callback < DataAttendance > () {
    @Override
    public void onResponse(Call < DataAttendance > call, Response < DataAttendance > response) {
     Log.i(TAG, "Response code: " + response.raw().code() + "\r\n" +
      "Request url: " + response.raw().request().url() + "\r\n");
     if (!response.isSuccessful() || response.body() == null) {
      try {
       Log.e(TAG, response.errorBody().string());
      } catch (IOException e) {
       e.printStackTrace();
      }
      return;
     }

     DataAttendance dataAttendance = response.body();

     if (dataAttendance.getStatusCode() == 0) {
      return;
     }

     Toast.makeText(context, "Attendance ID: " + dataAttendance.getStatusCode(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call < DataAttendance > call, Throwable t) {
     Log.e(TAG, "onFailure: " + t);
    }
   });
  }

  @Override
  public void onError(@NonNull Throwable throwable) {

  }
 }, false);
}

private RequestBody requestBody(String value) {
 return RequestBody.create(MediaType.parse("text/plain"), value);
}

Is there anybody willing to help me? I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant