dongliang2058 2018-01-31 05:30
浏览 77
已采纳

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

I am working on Android along with yii-2 php. From my app I am sending some photos in a file using an api call. Along with it I am sending a reference number as shown below.

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

The call is initialized as shown below

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");
                    }
                }

Working of app

  1. User note down the details of installation and take pictures
  2. On closing the form two API's are called i) API to save/upload installation data to server ii) API to save/upload images to the server.

The images are uploaded when the Installation API response is returned OK.

OkHttp Log

When the images are pushed to the server below is the call in 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

Server Side

At server side below is API code through which images are saved.

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()];
    }
}

The response after var_dump($dd) below is the response that generates.

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

In response there is no reference number. How can I get the reference number at server side?

Any help would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • duandu1966 2018-01-31 07:23
    关注

    Check if there is $_POST['ref_no'] present at server side after the call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同