服务器的JSON响应可能无效

时间:2015-12-07 08:45:36

标签: java android json loopj

我为此感到困惑,我不明白问题出在哪里。 该API已经过Postman测试成功,但我的应用程序失败了。

我的代码是:

public void invokeWS(RequestParams params) {
    // Show Progress Dialog
    prgDialog.show();
    // Make RESTful webservice call using AsyncHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    client.post("http://openetizen.com/api/v1/sessions", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            // Hide Progress Dialog
            prgDialog.hide();
            try {
                // JSON Object
                JSONObject obj = new JSONObject(String.valueOf(responseBody));
                Log.v("Response", String.valueOf(responseBody));
                // When the JSON response has status boolean value assigned with true
                if (obj.getBoolean("status")) {
                    Toast.makeText(getApplicationContext(), "Selamat datang!", Toast.LENGTH_LONG).show();
                    // Navigate to Home screen
                    navigatetoMainActivity();
                }
                // Else display error message
                else {
                    errorMsg.setText(obj.getString("error_msg"));
                    Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
                Log.e("ERROR", "Response");


            }
        }

        @Override
        protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
            return super.obtainMessage(responseMessageId, responseMessageData);
        }

        @Override
        public Object getTag() {
            return super.getTag();
        }

        @Override
        public void setTag(Object TAG) {
            super.setTag(TAG);
        }

        @Override
        public URI getRequestURI() {
            return super.getRequestURI();
        }

        @Override
        public void setRequestURI(URI requestURI) {
            super.setRequestURI(requestURI);
        }

        @Override
        public Header[] getRequestHeaders() {
            return super.getRequestHeaders();
        }

        @Override
        public void setRequestHeaders(Header[] requestHeaders) {
            super.setRequestHeaders(requestHeaders);
        }

        @Override
        public boolean getUseSynchronousMode() {
            return super.getUseSynchronousMode();
        }

        @Override
        public void setUseSynchronousMode(boolean sync) {
            super.setUseSynchronousMode(sync);
        }

        @Override
        public boolean getUsePoolThread() {
            return super.getUsePoolThread();
        }

        @Override
        public void setUsePoolThread(boolean pool) {
            super.setUsePoolThread(pool);
        }

        @Override
        public String getCharset() {
            return super.getCharset();
        }

        @Override
        public void setCharset(String charset) {
            super.setCharset(charset);
        }

        @Override
        public void onProgress(long bytesWritten, long totalSize) {
            super.onProgress(bytesWritten, totalSize);
        }

        @Override
        public void onStart() {
            super.onStart();
        }

        @Override
        public void onFinish() {
            super.onFinish();
        }

        @Override
        public void onPreProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
            super.onPreProcessResponse(instance, response);
        }

        @Override
        public void onPostProcessResponse(ResponseHandlerInterface instance, HttpResponse response) {
            super.onPostProcessResponse(instance, response);
        }

        @Override
        public void onRetry(int retryNo) {
            super.onRetry(retryNo);
        }

        @Override
        public void onCancel() {
            super.onCancel();
        }

        @Override
        public void onUserException(Throwable error) {
            super.onUserException(error);
        }

        @Override
        protected void handleMessage(Message message) {
            super.handleMessage(message);
        }

        @Override
        protected void sendMessage(Message msg) {
            super.sendMessage(msg);
        }

        @Override
        protected void postRunnable(Runnable runnable) {
            super.postRunnable(runnable);
        }

        @Override
        public void sendResponseMessage(HttpResponse response) throws IOException {
            super.sendResponseMessage(response);
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }

        @Override
        public boolean equals(Object o) {
            return super.equals(o);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public String toString() {
            return super.toString();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            // Hide Progress Dialog
            prgDialog.hide();
            // When Http response code is '404'
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            }
            // When Http response code is '500'
            else if (statusCode == 500) {
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            }
            // When Http response code other than 404, 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();
            }
        }
    });
}

此处发生错误:

catch (JSONException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
                Log.e("ERROR", "Response");


            }

API Tested Success on Postman

Tested failure on Application

我该怎么做才能解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个,让我知道结果

try 
{
     JSONObject obj = new JSONObject(new String(responseBody));
     if (obj.getString("status").equalsIgnoreCase("success") {
                    Toast.makeText(getApplicationContext(), "Selamat datang!", Toast.LENGTH_LONG).show();
     navigatetoMainActivity();
}
else {
    errorMsg.setText(obj.getString("error_msg"));
    Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
}