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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛