【问题标题】:Android AsyncTask with JSON Parsing error带有 JSON 解析错误的 Android AsyncTask
【发布时间】:2015-06-15 02:51:05
【问题描述】:

此部分出现错误:

protected void onPostExecute(JSONObject json) {
    try {
        // Getting JSON Array
        user = json.getJSONArray(TAG_USER);
        JSONObject c = json.getJSONObject(0);
        String id = c.getString(TAG_ID);
        String name = c.getString(TAG_NAME);
        Log.i("id",id);
    }
}

-------网络服务结果---

{"GetDataResult":{"ID":8,"Name":"Foo Bar"}} 工作链接 - http://127.0.0.1/WcfService4/Service1.svc/getData/?key=8

为解决这个问题提供更好的解决方案。

【问题讨论】:

  • 不是 127.0.0.1 本地主机吗?我们应该如何访问该链接?
  • “为解决这个问题提供更好的解决方案”.. 所以现在 StackOverflow 为你工作了呵呵..

标签: java android json web-services wcf


【解决方案1】:

看来你还没有使用你的 JSONArray 对象

JSONArray mainfoodlist = null;


    tipshealth = json.getJSONArray(TAG_RESPONSE);

        // looping through All RESPONSE
        for (int i = 0; i < tipshealth.length(); i++) {
        JSONObject jsonobj = tipshealth.getJSONObject(i);
        tipHealth = jsonobj.getString(KEY_HEALTHTIPS);

        listhealthtips.add(tipshealth.getJSONObject(i).getString("tips"));

        }

【讨论】:

    【解决方案2】:

    看来问题出在这一段:

    user = json.getJSONArray(TAG_USER);
    JSONObject c = json.getJSONObject(0);
    

    你得到一个JOSNObject user,但你从未使用过它。

    我想这可能是你的意思:

    user = json.getJSONArray(TAG_USER);
    JSONObject c = user.getJSONObject(0);
    

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多