【问题标题】:Sent parameter through PATCH android通过PATCH android发送参数
【发布时间】:2015-11-24 07:36:12
【问题描述】:

当我点击保存按钮时,它将使用参数 flexid 执行后台 PATCH 任务:

btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final String loc = flexlocationid.getText().toString().trim();
            Utils.log("loc: " + loc);
            SendfeedbackPatch jobpatch = new SendfeedbackPatch();
            jobpatch.execute(loc);
        }
});

后台任务是:

private class SendfeedbackPatch extends AsyncTask<String, Void, String> {
    private static final String LOG_TAG = "UserPreferedLocation";
    Bundle extras = getIntent().getExtras();
    final String token= extras.getString("TOKEN");
    @Override
    protected String doInBackground(String... params) {
        String flexid = params[0];
        Utils.log("flexid: " + flexid);
        final String url_patch_prefered_location = Constant.URI_BASE_FLEX;
        String contentType;
        contentType = "application/x-www-form-urlencoded";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPatch httpPatch= new HttpPatch(url_patch_prefered_location);
        httpPatch.setHeader("Content-Type", contentType);
        httpPatch.setHeader("Authorization", "Bearer " + token);
        httpPatch.setHeader("Accept", "application/json");
        httpPatch.setHeader("Accept-Charset", "utf-8");
        // do above Server call here
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("flex_id", flexid));
        try
        {
            HttpResponse response = httpClient.execute(httpPatch);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // EntityUtils to get the reponse content
                String content =  EntityUtils.toString(entity);
                Utils.log("daftar content: " + content);
                JSONObject hasiljson = new JSONObject(content);
                Utils.log("hasiljson object: " + hasiljson);
                String success = hasiljson.getString("success");
                Utils.log("success: " + success);
            }
            // writing response to log
            Log.d("Http Response:", response.toString());
        }
        catch (Exception e)
        {
            /*Toast.makeText(context,
                    "user not registered", Toast.LENGTH_SHORT).show();*/
            Log.e(LOG_TAG, String.format("Error during login: %s", e.getMessage()));
        }
        return "processing";
    }

    @Override
    protected void onPostExecute(String message) {
        //process message
        Toast.makeText(context,
                "Prefered training location updated", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(context, home.class);
        intent.putExtra("TOKEN", token);
        startActivity(intent);
        finish();
    }
}

我得到了 flexid 使用日志 Utils.log("flexid:" + flexid);,当我记录 Utils.log("daftar content:" + content); 时发现问题,它返回:daftar content: {" errors":{"flex_id":["flex id字段是必填项。"]}},表示flex_id没有填好。如何纠正这样 flex_id 被正确填充(补丁)?

【问题讨论】:

    标签: java android patch


    【解决方案1】:

    这里:

    content: {"errors":{"flex_id":["The flex id field is required."]}}
    

    因为用flex_id创建了BasicNameValuePair,但没有通过调用HttpPatchsetEntity方法在HTTP请求中设置。这样做:

    ....
    httpPatch.setEntity(new UrlEncodedFormEntity(nameValuePair));
    HttpResponse response = httpClient.execute(httpPatch);
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 2014-06-25
      • 2013-06-21
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多