【问题标题】:android JSONException while trying to parse JSON data尝试解析 JSON 数据时出现 android JSONException
【发布时间】:2013-01-03 10:12:50
【问题描述】:

我使用下面的类来获取一些 JSON 数据。奇怪的是,我不能只从我需要测试应用程序的 url 解析 JSON(服务器端不是我维护的):如果你点击 this link,你将能够在浏览器中看到 JSON 数据.但是当我尝试以编程方式解析它时,它会抛出 JSONException。

  01-03 11:08:23.615: E/JSON Parser(19668): Error parsing data org.json.JSONException:    Value <html><head><title>JBoss of type java.lang.String cannot be converted to JSONObject

我尝试使用http://ip.jsontest.com/http://api.androidhive.info/contacts 对其进行测试,效果很好!仅在尝试从我的第一个链接解析 JSON 时引发异常。我认为这可能是服务器问题,但我可以使用浏览器查看 JSON 数据.. 无法解释。

   public class JSONParser {

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

/* default constuctor*/
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

   /* get JSON via http request */
    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    /* try to parse the string to JSON Object*/
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

 /* return JSON String */
    return jObj;

      }
  }

【问题讨论】:

  • 看起来您实际上收到了 JBoss 错误消息。我会添加一个System.err.println(json); 以查看您实际尝试解析的内容...
  • 你的问题出在 jObj = new JSONObject(json); ,检查你的字符串'json'是否包含任何奇怪的符号或html。
  • 是的...值 ..........但是为什么呢?
  • 这个站点接受 POST 还是只接受 GET。我会尝试HttpGet httpGet = new HttpGet(url),因为这就是我们的浏览器检索 JSON 的方式

标签: android json exception


【解决方案1】:

使用 BufferedReader 方法将为您获取该页面的 HTML 源代码,这就是为什么您会在字符串中看到像

这样的 HTML 标记。相反,使用 EntityUtils 来获取 JSON 字符串: <pre><code>DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); int status = httpResponse.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { String jsonString = EntityUtils.toString(httpEntity); try { JSONObject jsonObject = new JSONObject(jsonString); } catch (JSONException e) { e.printStackTrace(); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } </code></pre>

【讨论】:

  • 当我尝试.....在 jsonString ::>>> 我没有得到整个数据......你知道如何管理它吗?
  • , "medium": { "url": "i.ytimg.com/vi/zUaMzFrRLWg/mqdefault.jpg", "width... "最后我得到了 "..." 如何获取整个数据"
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多