从客户端到服务器接收字符串值

时间:2018-01-31 05:30:34

标签: php android api file yii2

我正在Androidyii-2 php一起工作。从我的app我使用api电话在文件中发送一些照片。除此之外,我发送的参考号如下所示。

@Multipart
@POST("installation/photo/save")
Call<ApiResponse> uploadImage(@Header("Authorization") String token, @Part("ref_no") RequestBody ref_no, @Part MultipartBody.Part file);

呼叫初始化如下所示

Retrofit retrofit = RetrofitClient.getClient();

RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);

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

MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);

RequestBody ref_no = createPartFromString("<ref_no>");

ref_no = createPartFromString(installationDetails.getReferenceNo());

Call<ApiResponse> call = retrofitInterface.uploadImage("Bearer " + Common.getAuthKey(mContext),ref_no, body);

            call.enqueue(new Callback<ApiResponse>() {
                @Override
                public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                    if (response.isSuccessful()) {

                        Log.d(TAG, response.body().getStatus());

                        if (response.body().getStatus().equals("OK")) {

                            snapManager.updateSnapStatus(AssetsManagementContract.SnapEntry.COLUMN_SITE_SNAP, snap.getSnapName(), Constants.SNAP_SYNCED);
                            Intent broadcastSyc = new Intent();
                            broadcastSyc.setAction(Common.GetSyncImageAction());
                            broadcastSyc.putExtra("STATUS", true);
                            mContext.sendBroadcast(broadcastSyc);
                            sendImage(mContext);

                        }
                        else{
                            snapManager.updateSnapStatus(AssetsManagementContract.SnapEntry.COLUMN_SITE_SNAP, snap.getSnapName(), Constants.SNAP_CLOSED);
                        }

                    } else {
                        snapManager.updateSnapStatus(AssetsManagementContract.SnapEntry.COLUMN_SITE_SNAP, snap.getSnapName(), Constants.SNAP_CLOSED);
                        Log.d(TAG, "Error");
                    }
                }

使用应用

  1. 用户注意安装和拍照的详细信息
  2. 关闭表单时会调用两个API i)用于将安装数据保存/上传到服务器的API ii)用于保存/上传图像到服务器的API。
  3. 在返回安装API响应OK时上传图像。

    OkHttp Log

    当图像被推送到下面的服务器时,是ok http

    中的呼叫
    --> POST http://ip:port/api/web/v1/installation/photo/save
    01-31 08:34:14.723 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: multipart/form-data; boundary=704cd1e5-e4d5-4d2e-be63-81f5fe3f1aef
    01-31 08:34:14.723 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 116027
    01-31 08:34:14.724 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Authorization: Bearer key
    01-31 08:34:14.745 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --704cd1e5-e4d5-4d2e-be63-81f5fe3f1aef
    01-31 08:34:14.747 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Disposition: form-data; name="ref_no"
    01-31 08:34:14.748 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Transfer-Encoding: binary
    01-31 08:34:14.748 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: multipart/form-data; charset=utf-8
    01-31 08:34:14.754 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 15
    01-31 08:34:14.755 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: 28372250046142R //this is reference number
    01-31 08:34:14.755 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --704cd1e5-e4d5-4d2e-be63-81f5fe3f1aef
    01-31 08:34:14.755 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Disposition: form-data; name="uploaded_file"; filename="28372250046142R_1517369623_site_1.jpg"
    01-31 08:34:14.755 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: multipart/form-data
    01-31 08:34:14.755 5762-6086/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 115567
    

    服务器端

    在下面的服务器端是保存图像的API代码。

    public function actionSavephoto()
    {
        try {
            $count = 0;
            foreach ($_FILES as $f) {
                $dd = pathinfo($f['name']);
                if (!isset($dd['extension']) || !in_array($dd['extension'], array('jpg', 'png', 'gif'))) {
                    return ['status' => 'ERROR', 'uploaded_files' => $count, 'message' => 'Invalid File'];
                    break;
                }
                if (move_uploaded_file($f['tmp_name'], Installations::UPLOAD_FOLDER . $f['name'])) {
                    $count++;
                    return ['status' => 'OK', 'uploaded_files' => $count];
                    break;
                } else {
                    return ['status' => 'ERROR', 'uploaded_files' => $count];
                    break;
                }
    
            }
    
        } catch (Exception $x) {
            return ['status' => 'ERROR', 'message' => $x->getMessage()];
        }
    }
    

    以下var_dump($dd)之后的响应是生成的响应。

    array(4) {
      ["dirname"]=>
       string(1) "."
       ["basename"]=>
       string(37) "28372230019211U_1517370655_site_1.jpg"
       ["extension"]=>
         string(3) "jpg"
       ["filename"]=>
       string(33) "28372230019211U_1517370655_site_1"
      }
    

    作为回应,没有reference number。如何在服务器端获取参考号?

    任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

检查通话后服务器端是否有$_POST['ref_no']