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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵