不要求成功和失败

时间:2017-06-09 16:38:47

标签: android post asynchttpclient

虽然服务器收到了请求,但我不知道为什么此代码不会调用onSuccessonFailure。请帮忙。

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

0 个答案:

没有答案