1

When I click save button, it will do background PATCH task with a parameter flexid:

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

and the background task is :

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

I got that flexid use log Utils.log("flexid: " + flexid);, I found problem when I log Utils.log("daftar content: " + content);, it returns : daftar content: {"errors":{"flex_id":["The flex id field is required."]}}, it means the flex_id did not filled well. How to correct that so flex_id is filled (patch) properly?

1 Answers1

0

Here:

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

Because created BasicNameValuePair with flex_id but not setting in HTTP request by calling setEntity method of HttpPatch. do it as:

....
httpPatch.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPatch);
...
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213