Android中不完整的JSON解析
我试图从Google Civic的以下URL解析JSON,但Android并没有返回所有数据 具体地说,当“whilereadline”循环读取提要顶部时,我注意到它忽略了开头的括号和“normalizedInput”短语。我的代码如下:Android中不完整的JSON解析,android,json,api,Android,Json,Api,我试图从Google Civic的以下URL解析JSON,但Android并没有返回所有数据 具体地说,当“whilereadline”循环读取提要顶部时,我注意到它忽略了开头的括号和“normalizedInput”短语。我的代码如下: class GetRepresentatives extends AsyncTask<String,Void,Representative> { HttpURLConnection connector=null; JSONObj
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;
}
}
类GetTask
{
HttpURLConnection连接器=null;
JSONObject rawRepresentativeData;
字符串googleURL=”https://www.googleapis.com/civicinfo/v2/representatives?key=AIzaSyDfeiCRXoUEb2ZNaq9WmgadSmeEKAiCIlw&address=";
@凌驾
受保护代表背景(字符串…字符串){
字符串rawData=“”;
试一试{
URL currentInput=新URL(谷歌URL+“TX”);
连接器=(HttpURLConnection)currentInput.openConnection();
connector.connect();
BufferedReader=new BufferedReader(new InputStreamReader(connector.getInputStream());
StringBuilder=新的StringBuilder();
while(reader.readLine()!=null){
System.out.println(reader.readLine());
}
reader.close();
System.out.println(原始数据);
rawRepresentativeData=新的JSONObject(rawData);
System.out.println();
}
捕获(例外iiee)
{
System.out.println(iiee.toString());
}
返回null;
}
}
您没有正确使用BufferedReader
每次检查reader.readLine()时,都会读取一行您正在丢弃的内容,这就是为什么会丢失这些行
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}