通过PATCH android发送参数

时间:2015-11-24 07:36:12

标签: java android patch

当我单击“保存”按钮时,它将使用参数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();
    }
}

我得到了灵活的使用日志Utils.log(&#34; flexid:&#34; + flexid);我在登录Utils.log时发现问题(&#34; daftar内容:&#34; + content);,它返回:daftar内容:{&#34;错误&#34;:{&#34; flex_id&#34;:[&#34; flex id字段是必需的。&#34;]}},这意味着flex_id没有很好地填充。如何纠正,以便flex_id正确填充(补丁)?

1 个答案:

答案 0 :(得分:0)

下面:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

因为content: {"errors":{"flex_id":["The flex id field is required."]}} 已创建BasicNameValuePair,但未通过调用flex_id setEntity方法设置HTTP请求。这样做:

HttpPatch