【发布时间】:2018-03-05 15:18:45
【问题描述】:
我正在尝试从 Google Civic 的以下 URL 解析 JSON,但 Android 并未返回所有数据。
具体来说,当“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;
}
}
【问题讨论】: