1
private void Dialog_profile_pic() {
    // create upload service client
    File file = new File(selectedImagePath);

    // create RequestBody instance from file
    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("memFile", file.getName(), requestFile);

    // add another part within the multipart request
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), ApiResultCode.getApiKey());

    RequestBody description2 =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), ApiResultCode.getApiType());

    Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
            .loginPicture(description, description2, body);

    loginPictureCall.enqueue(new Callback<LoginPicture>() {
        @Override
        public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
            //CONNECTION SUCCESS
            LoginPicture NewUser = response.body();
            if (NewUser.getResponsedata().getResultCode() == 100) {
                Log.e("DEBUG", "CONNECTION result: CONGRATS");
            } else {

                Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
            }
        }

        @Override
        public void onFailure(Call<LoginPicture> call, Throwable t) {
            //CONNECTION FAIL
            Log.e("DEBUG", "CONNECTION result: FAIL" );
        }
    });
}

\

public interface ApiInterface {
@Multipart
    @POST("/memberController/joinUploadProfile.json")
    Call<LoginPicture> loginPicture(@Part("apiKey") RequestBody apiKey, @Part("apiType") RequestBody apiType, @Part("memFile") MultipartBody.Part file); //multi part

\ I am trying to pass in two string parameters and an image file using retrofit2 but I am failing to get a connection. Anyone can help me find what I need to do? I have been struggling over this for 2 days now.

3
  • Please remove Android Studio from the subject as it has nothing to do with your problem.
    – greenapps
    Mar 17, 2016 at 7:13
  • If you don't get a connection then how do you know? Please tell how your code flows and which error messages and exceptions you get.
    – greenapps
    Mar 17, 2016 at 7:19
  • @greenapps it is related to Android Studio. and the Log messages that I set up told me which connection errors I am getting. no thank you.
    – Jisu Hong
    Mar 21, 2016 at 5:21

1 Answer 1

-1

//In Request Client API Interface

@Multipart
    @POST("URL.json")
    Call<LoginPicture> loginPicture(@PartMap() Map<String, RequestBody> mapPhoto); //multi part

//In activity

private void Dialog_profile_pic(final Uri selectedImageUri) {
    // create upload service client
    File file = new File(selectedImagePath);
    HashMap<String, RequestBody> map = new HashMap<>();
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("text/plain"),"content1");
    RequestBody description2 =
            RequestBody.create(
                    MediaType.parse("text/plain"), "content2");

    // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("image/jpeg"), file);
    map.put("memFile\"; filename=\""+file.getName(),requestFile);
    map.put("apiKey",description);
    map.put("apiType", description2);

    Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
            .loginPicture(map);
    loginPictureCall.enqueue(new Callback<LoginPicture>() {
        @Override
        public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
                Picasso.with(Activity_create.this).load(NewUser.getResponsedata().getResultObject()).into(iv_profile_pic);
            } else {
                Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
            }
        }

        @Override
        public void onFailure(Call<LoginPicture> call, Throwable t) {
            //통신 실패 시
            Log.e("DEBUG", "CONNECTION result: FAIL");
        }
    });
}

//And it works now

3
  • You are very rude with people that try to help you
    – mromer
    Mar 21, 2016 at 7:55
  • greenapps was right. Can you say me where the relationship with Android Studio is in this issue? And also adding logs allow people to help you on a effective way
    – mromer
    Mar 21, 2016 at 8:55
  • This is from Android Studio and I did not have any compile error logs. The program was not outputting what I wanted. Do you need more help?
    – Jisu Hong
    Mar 22, 2016 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.