Retrofit没有向服务器

时间:2016-12-15 10:09:05

标签: android image-uploading retrofit2 android-image

以下是我用于将图片上传到服务器的代码。但它没有像user_id那样发送正确的值,因此它显示了错误的结果。当我在镀铬上打开它的工作正常时它给出了正确的结果。我使用代码上传图片是:

@Multipart
@POST("/queli_technologies/index.php/Webservice")
Call<EditProfileResponse> editProfile(@Part("u_id") String firstname,
                                    @Part("f_name") String lastname,
                                    @Part("l_name") String email,
                                    @Part("c_no") String password,
                                    @Part MultipartBody.Part file,
                                    @Part("edit_profile") String register);

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

                service = RetroClient.getApiService();


                Call<EditProfileResponse> responseCall = service.editProfile("44","Demo","android","0987654321",body,"edit_profile");

                responseCall.enqueue(new Callback<EditProfileResponse>() {
                    @Override
                    public void onResponse(Call<EditProfileResponse> call, Response<EditProfileResponse> response) {
                        if (response.isSuccessful()){
                            EditProfileResponse res = response.body();
                            Log.e("Response " , res.getStatus() + " message : " + res.getMessage());
                            dialog.dismiss();
                        }
                    }

                    @Override
                    public void onFailure(Call<EditProfileResponse> call, Throwable t) {

                    }
                });

当我没有得到正确的结果时,我使用它:

@Multipart
@POST("/queli_technologies/index.php/Webservice")
Call<EditProfileResponse> editProfile(@Part("u_id") RequestBody userId,
                                      @Part("f_name") RequestBody frstname,
                                      @Part("l_name") RequestBody lastname,
                                      @Part("c_no") RequestBody contctnum,
                                      @Part MultipartBody.Part file,
                                      @Part("edit_profile") RequestBody edit);


   File file = new File(BaseActivity.basicImagePath);
   RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);

                service = RetroClient.getApiService();
                String text = "31";
                RequestBody useridbody =RequestBody.create(MediaType.parse("text/plain"), text);
                RequestBody fnbody =RequestBody.create(MediaType.parse("text/plain"), "Demo");
                RequestBody lnbody =RequestBody.create(MediaType.parse("text/plain"), "android");
                RequestBody conbody =RequestBody.create(MediaType.parse("text/plain"), "0987654321");
                RequestBody isbody =RequestBody.create(MediaType.parse("text/plain"), "edit_profile");

                Call<EditProfileResponse> responseCall = service.editProfile(useridbody,fnbody,lnbody,conbody,body,isbody);

                responseCall.enqueue(new Callback<EditProfileResponse>() {
                    @Override
                    public void onResponse(Call<EditProfileResponse> call, Response<EditProfileResponse> response) {
                        if (response.isSuccessful()){
                            EditProfileResponse res = response.body();
                            Log.e("Response " , res.getStatus() + " message : " + res.getMessage());
                            dialog.dismiss();
                        }
                    }

                    @Override
                    public void onFailure(Call<EditProfileResponse> call, Throwable t) {
                        Log.e("Failure Response " , t +"");
                    }
                });

仍然无法得到正确的结果。当通过改造使用时,Log中的Api响应给出{"status":"0","message":"User doesnot exist"}。从 服务器显示{"status":"1","message":"User profile Updated"}

这是改造问题还是我的编码问题?

1 个答案:

答案 0 :(得分:0)

将文件移至最后。

@POST("/queli_technologies/index.php/Webservice")
Call<EditProfileResponse> editProfile(@Part("u_id") String firstname,
                                @Part("f_name") String lastname,
                                @Part("l_name") String email,
                                @Part("c_no") String password,                                    
                                @Part("edit_profile") String register,
                                @Part MultipartBody.Part file);

我认为它应该有用。