【问题标题】:How to handle 500 Internal Server Error when using JSON使用 JSON 时如何处理 500 内部服务器错误
【发布时间】:2015-02-28 20:18:06
【问题描述】:

我对 Android 比较陌生,我正在使用 JSON 从服务器获取数据。在第 22 行的第一个循环中,StringBuilder 包含 500 Internal Server Error,然后 jArray 最终返回 null。我该如何处理这个错误?

public static JSONObject getJSON() {
    String jsonString = "";
    InputStream inStream = null;

    //http post
    JSONObject jArray = null;
    try {
        HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httpPost = new HttpPost(WS_URL);
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        inStream = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        inStream.close();
        jsonString = sb.toString();

        jArray = new JSONObject(jsonString);


        //outputTransactions(jArray);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jArray;
}

【问题讨论】:

  • 你能发布你想要获取的json字符串吗

标签: android json


【解决方案1】:

虽然回复较晚,但可能对其他人有所帮助。在将其解析为 JSON 之前,您需要检查服务器的响应状态。

例如

int status_code=response.getStatusLine().getStatusCode();

if(status_code!=200){
    Log.d("MYLOG","ERROR! Response status is"+status_code);
  }
else{
    inStream = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    inStream.close();


   // Rest of your code......
    }

或者您可以选择检查状态代码并向用户显示错误

喜欢:

else if(status_code==404){
 Log.d("MYLOG","Sorry! Page not found! Check the URL ");

}else if(status_code==500){
 Log.d("MYLOG","Server is not responding! Sorry try again later..");
}

希望它对像你这样的新手有所帮助:-)

【讨论】:

    【解决方案2】:

    看看这个答案:https://stackoverflow.com/a/8148785/1974614

    您应该检查statusCode 与 500

    【讨论】:

      【解决方案3】:

      “500 内部服务器”错误表示服务器在响应您的请求时出现问题。您没有收到 JSON 字符串响应。

      然后,当您尝试创建 jArray 时,字符串不是有效的 JSON,JSONObject 无法解析它 - 正如您所说,它返回“null”。

      您可以解析服务器响应以查看它是否包含此字符串,然后创建您想要的任何 jArray 对象,但您无法从非 JSON 字符串中获取 JSON 对象。

      【讨论】:

        【解决方案4】:

        您应该考虑使用库来处理 REST 请求,例如:http://square.github.io/retrofit/

        如果您使用这样的库,您可以在成功响应可用时从 json 获取对象,而在发生错误时获取其他对象。

            MyApi mylogin = restAdapter.create(MyApi.class); //this is how retrofit create your api
            mylogin.login(username,password,new Callback<String>() {
                @Override
                public void success(String s, Response response) {
                    //process your response if login successfull you can call Intent and launch your main activity
        
                }
        
                @Override
                public void failure(RetrofitError retrofitError) {
                    retrofitError.printStackTrace(); //to see if you have errors
                }
            });
            }
        

        【讨论】:

          【解决方案5】:

          我遇到了和你一样的问题,我解决了它,因为我在添加 GSON .jar 文件以添加我的服务器端项目时错过了一部分。我认为您也应该像我一样小心地将外部库添加到您的项目中。通过这些链接,我可以意识到问题。

          LINK 1

          LINK 2

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-02
            • 1970-01-01
            • 2018-07-08
            • 2014-01-28
            相关资源
            最近更新 更多