Android中不完整的JSON解析

问题描述 投票:-3回答:1

我正在尝试从Google Civic的以下网址解析JSON,但Android并未返回所有数据。

https://www.googleapis.com/civicinfo/v2/representatives?key=AIzaSyDfeiCRXoUEb2ZNaq9WmgadSmeEKAiCIlw&address=TX

具体来说,当“while-readLine”循环读取时,在feed的顶部,我注意到它忽略了左括号和“normalizedInput”短语。我的代码如下:

class GetRepresentatives extends AsyncTask<String,Void,Representative>
{

    HttpURLConnection connector=null;
    JSONObject rawRepresentativeData;
    String googleURL="https://www.googleapis.com/civicinfo/v2/representatives?key=AIzaSyDfeiCRXoUEb2ZNaq9WmgadSmeEKAiCIlw&address=";

    @Override
    protected Representative doInBackground(String... strings) {


        String rawData="";

        try {
            URL currentInput = new URL(googleURL + "TX");

            connector=(HttpURLConnection) currentInput.openConnection();

            connector.connect();

            BufferedReader reader=new BufferedReader(new InputStreamReader(connector.getInputStream()));

            StringBuilder builder=new StringBuilder();

            while(reader.readLine() != null) {
                System.out.println(reader.readLine());
            }



            reader.close();

            System.out.println(rawData);
            rawRepresentativeData=new JSONObject(rawData);

            System.out.println();



        }
        catch(Exception iiee)
        {
            System.out.println(iiee.toString());
        }

        return null;
    }
}
android json api
1个回答
1
投票

您没有正确使用BufferedReader!

每次检查reader.readLine()时都会读到一条你丢弃的行,这就是缺少行的原因。

 String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
© www.soinside.com 2019 - 2024. All rights reserved.