0

I don't know why this code doesn't call onSuccess and onFailure although server received the request. Please help.

static AsyncHttpClient client = new AsyncHttpClient();

private void invokeWS(JSONObject params,String abc){

    StringEntity entity = null;
    try {
        entity = new StringEntity(params.toString());
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        entity.setContentEncoding(new BasicHeader("X-Openerp-Session-Id",abc));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    client.post(getApplicationContext(), "http://192.168.56.1:8069/api/notification", entity, "application/json", new JsonHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) {
            try {
                if (responseBody.has("result")) {
                    Log.d("bbb1",String.valueOf(responseBody));
                    String gresult = responseBody.getString("result");
                    JSONObject jsonObject = new JSONObject(gresult);
                    noti = jsonObject.getString("notifications");
                } else {
                    Toast.makeText(getApplicationContext(), responseBody.getString("error_msg"), Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable error) {
            super.onFailure(statusCode, headers, responseString, error);
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            }
            // When Http response code is '500'
            else {
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
            }
        }
    });
}
Felix
  • 571
  • 14
  • 34
Danh Danh
  • 3
  • 1
  • 6

0 Answers0