1

I am trying to register the user with an image using post method, the user has registered but the image not uploaded and no error.

I saw many similar problems but didn't get the solution.

The image uploaded successfully from chrome extension rest api request and HTML form.

my code is below...

@Multipart
@POST("webservicename")
Call<Login> userUpdate1(@Part("device_id") RequestBody deviceid,
                        @Part("ip_address") RequestBody ipaddress,
                        @Part("user_id") RequestBody userid,
                        @Part("profile_images") MultipartBody.Part profile_images,
                        @Part("bdate") RequestBody bdate,
                        @Part("mangal_sani") RequestBody mangal_sani,
                        @Part("spect") RequestBody spect,
                        @Part("merried_status") RequestBody merried_status,
                        @Part("name") RequestBody name,
                        @Part("height") RequestBody height,
                        @Part("weight") RequestBody weight,
                        @Part("qualification") RequestBody qualification,
                        @Part("occupation") RequestBody occupation,
                        @Part("income") RequestBody income,
                        @Part("native") RequestBody nativeplace,
                        @Part("father") RequestBody father,
                        @Part("mother") RequestBody mother,
                        @Part("brothers") RequestBody brothers,
                        @Part("sisters") RequestBody sisters,
                        @Part("fincome") RequestBody fincome,
                        @Part("about_me") RequestBody about_me,
                        @Part("address") RequestBody address,
                        @Part("email") RequestBody email,
                        @Part("mobile") RequestBody mobile,
                        @Part("phone") RequestBody phone,
                        @Part("job") RequestBody job,
                        @Part("age") RequestBody age);

my image upload coding is below

  private void imageupload() {
    final ProgressDialog pDialog = new ProgressDialog(getActivity());
    pDialog.setIndeterminate(true);
    pDialog.setMessage("Loding...");
    pDialog.setCancelable(false);
    if (!pDialog.isShowing()) {
        pDialog.show();
    }
    File file;
    if (selectedFilePath.length() > 0) {
        file = new File(selectedFilePath);
    } else {
        file = new File(imagename);
    }

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("profile_images", file.getName(), reqFile);
    RequestBody email = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edemailid.getText().toString());
    RequestBody mobile = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmobileno.getText().toString());
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edname.getText().toString());
    RequestBody occupation = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edoccupation.getText().toString());
    RequestBody qualification = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edqualification.getText().toString());
    RequestBody bdate = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.eddob.getText().toString());
    RequestBody father = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edfathername.getText().toString());
    RequestBody mother = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmothername.getText().toString());
    RequestBody sisters = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edsistername.getText().toString());
    RequestBody brothers = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edbrothersname.getText().toString());
    RequestBody fincome = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edfamilyincome.getText().toString());
    RequestBody job = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edjob.getText().toString());
    RequestBody income = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edincome.getText().toString());
    RequestBody height = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edheight.getText().toString());
    RequestBody weight = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edweight.getText().toString());
    RequestBody address = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edaddress.getText().toString());
    RequestBody phone = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edphone.getText().toString());
    RequestBody mangal_sani = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmangalsani.getText().toString());
    RequestBody age = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edage.getText().toString());
    RequestBody spect = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edspector.getText().toString());
    RequestBody about_me = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edaboutMe.getText().toString());
    RequestBody merried_status = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.spmerridstatus.getSelectedItem().toString());
    RequestBody nativeplace = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.ednativeplace.getText().toString());
    RequestBody DeviceId = RequestBody.create(MediaType.parse("text/plain"), generateID());
    RequestBody IpAddress = RequestBody.create(MediaType.parse("text/plain"), getIpAddress());
    RequestBody Userid = RequestBody.create(MediaType.parse("text/plain"), UserId);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ApiInterface service = retrofit.create(ApiInterface.class);
    Call<Login> req = service.userUpdate1(DeviceId, IpAddress, Userid, body, bdate, mangal_sani, spect, merried_status, name, height, weight, qualification, occupation, income, nativeplace, father, mother, brothers, sisters, fincome, about_me, address, email, mobile, phone, job, age);
    req.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {
            // Do Something
            Login mLoginObject = response.body();
            String returnedResponse = mLoginObject.status;
            String msg = mLoginObject.msg;
            String Userid = mLoginObject.user_id;
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
            if (returnedResponse.trim().equals("1")) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
            if (returnedResponse.trim().equals("2")) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
            if (returnedResponse.trim().equals("3")) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
            if (returnedResponse.trim().equals("4")) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
            if (returnedResponse.trim().equals("0")) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Login> call, Throwable t) {
            t.printStackTrace();
        }
    });
}
Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45

4 Answers4

2

In your Calling class create Multipart body as follows;

MultipartBody.Part body = MultipartBody.Part.createFormData("profile_images",file.getName(), RequestBody.create(MediaType.parse("image/*"),file));

here profile_images is parameter name of your file

In Your Interface use @Part MultipartBody.Part file instead @Part("profile_images") MultipartBody.Part profile_images so your code will be as follows:-

@Multipart
@POST("webservicename")
Call<Login> userUpdate1(@Part("device_id") RequestBody deviceid,
                        @Part("ip_address") RequestBody ipaddress,
                        @Part("user_id") RequestBody userid,
                        @Part MultipartBody.Part profile_images ...)

I hope its work for you.

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • my retrofit library version is old so it not upload i comiple new retrofit library then i get solution ..thanks for your response. – bhoomika patel Jul 12 '18 at 10:54
  • @bhoomikapatel problem was not library version it was problem was in your interface as per your code in your question – Saurabh Bhandari Jul 12 '18 at 11:02
  • but my old library version after @part it give error to pass value if i change library then it not give error in interface class and i have not pass value – bhoomika patel Jul 12 '18 at 11:13
1

Try this

In Your Method

MultipartBody.Part filepart = MultipartBody.Part.createFormData("photo",file.getName(), RequestBody.create(MediaType.parse("image/*"),file));
Call<User> call = client.createAccount(filepart);

In Your Api Interface

public interface UserClient {

    @Multipart
    @POST("apiupdateprofile.php")

    Call<User> createAccount(

            @Part MultipartBody.Part filepart

    );
}
shyam
  • 1,084
  • 12
  • 20
1

Try This:

RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
prashant17
  • 1,520
  • 3
  • 14
  • 23
1

Try this way f you want to upload image MediaType.parse("image/*")

MultipartBody.Part body = null;

            if (selectedImagePath != null) {
                File file = new File (selectedImagePath);
                RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                body = MultipartBody.Part.createFormData("keyWord", file.getName(), reqFile);

            }
Adil
  • 812
  • 1
  • 9
  • 29