使用JsonReader读取json url

时间:2015-03-27 23:18:16

标签: java android json

我正在尝试使用JsonReader读取json url!。一旦我拨打reader.beginArray(),我就会:

  

java.lang.IllegalStateException:预期为BEGIN_ARRAY但是为STRING。

这是json url:

http://www.metlink.org.nz/stop/nearbystopdata?lat=-41.278407655948&lng=174.77938892631

    @Override
    protected String doInBackground(String... urls) {

        String result = "";
        String url="http://www.metlink.org.nz/stop/nearbystopdata?lat=-41.278407655948&lng=174.77938892631";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        try {

            HttpResponse response = httpclient.execute(httppost);
            InputStream  in=response.getEntity().getContent();

            JsonReader reader ;

            reader= new JsonReader(new InputStreamReader(in, "UTF-8"));
            reader.setLenient(true);


             try {
                 listData=(ArrayList<DivanData>) readMessagesArray(reader);
             }
             finally {
               reader.close();
             }





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

        return result;
    }


      public ArrayList<DivanData> readMessagesArray(JsonReader reader) throws IOException {
             ArrayList<DivanData> messages = new ArrayList();

             try {
                 reader.beginArray();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             while (reader.hasNext()) {
               messages.add(readMessage(reader));
             }
             reader.endArray();
             return messages;
           }

我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于你有内容的HTML,为什么不尝试使用jsoup(http://jsoup.org/)从html获取json作为字符串。它非常简单,需要的代码更少:

$('div.a.b')