dragon012100 2018-03-21 07:31
浏览 134
已采纳

图片上传API Call适用于Postman,但为什么不使用Android App?

I am trying to upload an image through API call from my app. But it seems to me that it is working in postman but not when done through my app. I am using retrofit 2 and backend code is PHP.Kindly help me through the process.

PHP Code:

case 'profileImage':

$id=$_POST['id'];
$image_name=("profile".$id.".jpg");
$upload_path=("../gm-app/assets/images/users-profile/".$image_name);

    $res= mysqli_query($db,"UPDATE `users` SET `image` = ('$image_name') WHERE `userid`='".$_POST['id']."'");

    if(isset($_FILES['file'])){

         if(move_uploaded_file($_FILES['file']['tmp_name'], $upload_path)){

            $rs['message'] = "Profile Image Updated";
            $rs['stat'] = "SUCCESS"; 
    }else{

        $rs['message'] = "Profile Image not Updated";
        $rs['stat'] = "FAILED";

    }
    }

echo json_encode($rs);die;
    break;

Retrofit call:

File file = new File(mediaPath);
        RequestBody requestBody=RequestBody.create(MediaType.parse("image/*"), file);
        Retrofit retrofit=new Retrofit.Builder().
                baseUrl(MyConstants.ROOT_URL).
                addConverterFactory(ScalarsConverterFactory.create()).
                build();

        apinterface services = retrofit.create(apinterface.class);
        Call<ResponseBody> call=services.postImage(requestBody,uid);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.body()!=null)
                {

                    try {

                        Toast.makeText(MyProfile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
                        String image =  response.body().string();

                        Log.d(TAG, "onResponse: "+image);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });

API :

@Multipart
    @POST("services.php?apicall=profileImage")
    Call<ResponseBody> postImage(@Part("file") RequestBody image, @Part("id") String id);
  • 写回答

2条回答 默认 最新

  • dsbj66959 2018-03-21 07:47
    关注

    Because there's something wrong with your code about multipart..

    Interface :

    @Multipart
    @POST("services.php?apicall=profileImage")
    Call<ResponseBody> postImage(
        @Part MultipartBody.Part image, 
        @Part("id") RequestBody id
    );
    

    Call the API

    Retrofit retrofit=new Retrofit.Builder().
            baseUrl(MyConstants.ROOT_URL).
            addConverterFactory(ScalarsConverterFactory.create()).
            build();
    apinterface services = retrofit.create(apinterface.class);
    
    //Bikin request body
    File file = new File(mediaPath);
    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
    RequestBody reqId = RequestBody.create(MediaType.parse("text/plain"), uid);
    
    //Tembak APInya
    services.postImage(reqfile, reqId)
        .enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //tulis aksi disini
            }
    
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                //tulis aksi disini
            }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?
  • ¥15 谷歌地图是不是不开通结算功能,api会使用不了哦
  • ¥15 unity腾讯云对象存储机型适配
  • ¥15 求全国交通咨询模拟代码,要求如下,可以完全在dev c++运行
  • ¥15 根据要求修改程序编码
  • ¥15 用 Python 做一个用 Excel 表导入的答题系统
  • ¥15 使用微信开发者工具实现一个“婚博会”小程序
  • ¥15 ros的rviz仿真机器人
  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)