执行JSON请求时出现内部错误

时间:2011-03-09 14:45:21

标签: android json

我正在开发一个通过JSON消息与服务器通信的应用程序。

发送了一些消息并正确接收了结果。但我的一个例程抛出了一个错误,我不确定这是我的问题还是服务器。

错误是:

 org.apache.http.client.HttpResponseException: Internal Server Error
     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
     at br.com.iba.dao.DLS.post(DLS.java:121)
     at br.com.iba.dao.DLS.fulfill(DLS.java:151)
     at br.com.iba.LibraryActivity$3.doAction(LibraryActivity.java:123)
     at br.com.iba.LibraryActivity$SwitchListener.setSelected(LibraryActivity.java:208)
     at br.com.iba.LibraryActivity$SwitchListener.access$0(LibraryActivity.java:204)
     at br.com.iba.LibraryActivity$ButtonGroup.onClick(LibraryActivity.java:230)
     at android.view.View.performClick(View.java:2408)
     at android.view.View$PerformClick.run(View.java:8816)
     at android.os.Handler.handleCallback(Handler.java:587)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4627)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)

我发布的代码是:

    private String post(String json) {
        int TIMEOUT_MILLISEC = 10000; // = 10 seconds
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);

        HttpPost request = new HttpPost(
                "https://my server url");
        try {
            StringEntity entity = new StringEntity(json);
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            request.setEntity(entity);

            ResponseHandler<String> handler = new BasicResponseHandler();
            return client.execute(request, handler);
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }

我在BasicResponseHandler Doc上阅读:

“一个ResponseHandler,它将响应主体作为成功(2xx)响应的字符串返回。如果响应代码是> = 300,则响应主体被消耗并抛出HttpResponseException。如果这与{@link一起使用org.apache.http.client.HttpClient #execute(org.apache.http.client.methods.HttpUriRequest,ResponseHandler),HttpClient可以在内部处理重定向(3xx响应)。“

所以现在我认为这是一个服务器端错误。我需要知道究竟是哪个错误。

由于

1 个答案:

答案 0 :(得分:5)

您的服务器返回500内部服务器错误响应状态。所以问题出在您的服务器上。