我无法使用改造上传图片

时间:2018-07-12 09:43:27

标签: android retrofit

我正在尝试使用post方法向用户注册图片,该用户已注册,但图片未上传且没有错误。

我看到了许多类似的问题,但没有得到解决方案。

从chrome扩展rest api请求和HTML表单成功上传了图片。

我的代码在下面...

@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);

我的图片上传代码如下

  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();
        }
    });
}

4 个答案:

答案 0 :(得分:1)

尝试一下

使用您的方法

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

在您的Api界面中

public interface UserClient {

    @Multipart
    @POST("apiupdateprofile.php")

    Call<User> createAccount(

            @Part MultipartBody.Part filepart

    );
}

答案 1 :(得分:1)

尝试一下:

RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

答案 2 :(得分:1)

尝试这种方式来上传图片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);

            }

答案 3 :(得分:1)

在您的Calling类中,如下创建Multipart主体;

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

此处 profile_images 是文件的参数名称

在您的界面中使用@Part MultipartBody.Part file代替@Part("profile_images") MultipartBody.Part profile_images,因此您的代码如下:-

@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 ...)

希望它对您有用。