一个JSONArray文本必须以'['在...的字符1开头,这是完美的,但它始于'['

时间:2012-03-30 03:04:47

标签: android json

这非常令人困惑,突然间,以下代码无法正常工作。试图让我的Android显示网页已经很好地展示了:

HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost;
          httppost = new HttpPost(myUrl);              
          try 
          {                
              HttpResponse response = httpclient.execute(httppost);
              StringBuilder strResponse = inputStreamToString(response.getEntity().getContent());
              JSONArray arrWishlists = new JSONArray(strResponse.toString());
              .... 
              the rest is not important

在使用以下消息创建JSONArray时崩溃了,如果我的数组实际上并不是一个真正合法的JSON数组,当在同一页面上的javascript中使用时,它会非常有效试图从Android访问:

03-30 02:55:21.304: W/System.err(346): org.json.JSONException: A JSONArray text must start with '[' at character 1 of [{"Index":"296","Name":"\u041c\u0410\u0428\u0418\u041d\u042b","Description":"","VisibleToFriends":"1","Items":[{"Name":"porche 911","Description":"","Image":"http:\/\/www.tuningnews.net\/wallpaper\/1024x768\/porsche-911-carrera-s-coupe-01.jpg","Index":"0","Link":"http:\/\/www.autotrader.com\/research\/car-models\/2012-Porsche-911\/21675-336697-Coupe\/2dr-Cpe-S-Turbo~model.jsp","ReservedBy":"1225198689"},{"Name":"Ferrari","Description":"F150","Image":"http:\/\/4.bp.blogspot.com\/-szn3PGyHsC4\/TVPtm1Eh2HI\/AAAAAAAADiI\/58R4x_IaE9g\/s1600\/ferrari_f150_1680_8.jpg","Index":1,"Link":"http:\/\/en.wikipedia.org\/wiki\/Ferrari_150\u00b0_Italia","ReservedBy":"1374872217"},{"Name":"Lamborgini","Description":"Diablo","Image":"http:\/\/users1.ml.mindenkilapja.hu\/users\/ozdsuli\/uploads\/lamborgini.jpg","Index":2,"Link":"","ReservedBy":"1225198689"}]},{"Index":"341","Name":"Birthday","Description":"My birthday wishes","VisibleToFriends":"1","Items":[{"Name":"iPad 3 ","Description":"Need for speed","Image":"http:\/\/www.techieday.com\/wp-content\/uploads\/2011\/05\/ipad-3-release-date.jpg","Index":"0","Link":"","ReservedBy":"1225198689"},{"Name":"iOS Development License","Description":"100$","Image":"http:\/\/www.geek.com\/wp-content\/uploads\/2010\/09\/apps_20100901.jpg","Index":1,"Link":"http:\/\/developer.apple.com\/programs\/ios\/","ReservedBy":""}]}]

1 个答案:

答案 0 :(得分:0)

我不确定你在inputStreamToString中做了什么,但尝试这样做

private String convertToString(InputStream istr){
    if( istr == null ){
        return "";
    }
    try {
        return new java.util.Scanner(istr).useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
        return "";
    }
}

并在您的Http代码中

String strResponse = convertToString(response.getEntity().getContent());
JSONArray arrWishlists = new JSONArray(strResponse);