我正在尝试从Google Civic的以下网址解析JSON,但Android并未返回所有数据。
具体来说,当“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;
}
}
您没有正确使用BufferedReader!
每次检查reader.readLine()时都会读到一条你丢弃的行,这就是缺少行的原因。
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}