【问题标题】:Json object, what to return on http errorJson 对象,http 错误返回什么
【发布时间】:2014-08-10 22:51:29
【问题描述】:

我遇到了一个简单的问题

在我的活动中,我进行了异步函数调用

json = jsonParser.post(URL, params);

我的类+函数是这样的

public class json {

static InputStream stream = null;
static JSONObject jObj = null;
static String json = "";

public json() {
    // TODO Auto-generated constructor stub
}

public JSONObject post(String url, List<NameValuePair> NameValuePairs) {
    try {

        String paramString = URLEncodedUtils
                .format(NameValuePairs, "utf-8");

        Log.d("AsyncTask","1:Startet http");

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        Log.d("AsyncTask","1: http Post");
        httpPost.setEntity(new UrlEncodedFormEntity(NameValuePairs));
        Log.d("AsyncTask","1: send http.post");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.d("AsyncTask", " receive Response");

        // make sure response is good (Int 200)
        if(httpResponse.getStatusLine().getStatusCode() == 200){

        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
        Log.d("AsyncTask", " get Entity");
        stream = httpEntity.getContent();

        json = convertStreamToString(stream);
        Log.d("AsyncTask", "Convert Json");
        stream.close();
        } else {
            Log.d("AsyncTask","1: no internet connection");
        }
        Log.d("AsyncTask", "After Convert Json");
        } else {
            Log.e("AsyncTask: ", "server not found error code: " + String.valueOf(httpResponse.getStatusLine().getStatusCode()));
        }

    } catch (UnsupportedEncodingException e) {
        Log.d("AsyncTask","2: no internet connection");
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        Log.d("AsyncTask","3: no internet connection");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("AsyncTask","7: no internet connection");
        e.printStackTrace();
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("AsyncTask","error1");
        //Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    Log.e("AsyncTask","retun to getgame");
    //Log.d("JSON_POST", jObj.toString());
    // return JSON String
    return jObj; // <--- this is the problem
}

我的问题是当服务器没有响应 ok 时,我的函数返回 null。(使应用程序终止) 我想以某种方式捕捉到这一点并返回到我的异步,我想在其中发布带有错误代码的祝酒词...以便用户知道稍后再试。

谁能帮我解决这个问题?

【问题讨论】:

  • “我的函数返回 null。(使应用程序终止)”:所以基本上,只需检查返回是否为 null。要么使用“状态”代码创建您自己的 JSON 字符串,例如 {"status" : "Error"},然后检查。

标签: php android json http


【解决方案1】:

与其盲目返回jObj,不如尝试检查是否为null,如果为null,则分配一个新的空白JSONObject。将最后一行替换为:

if (jObj != null) {
    return jObj;
} else {
    return new JSONObject();
}

这至少应该消除空错误。

【讨论】:

  • 我最终使用了这个并添加了一个带有 error:1 的 Json 对象来检测我活动中的问题。非常感谢
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多