【问题标题】:Incomplete JSON Parsing in AndroidAndroid中不完整的JSON解析
【发布时间】:2018-03-05 15:18:45
【问题描述】:

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

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

具体来说,当“while-readLine”循环读取提要顶部时,我注意到它忽略了左括号和“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】:

    您没有正确使用 BufferedReader!

    每次您检查 reader.readLine() 时已经读取了您要丢弃的一行,这就是缺少行的原因。

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

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多