使用googlefeedapi-java
我正在尝试使用GoogleFeedAPI获取一些数据。但是line=reader.readLine()始终为空使用googlefeedapi-java,java,google-feed-api,Java,Google Feed Api,我正在尝试使用GoogleFeedAPI获取一些数据。但是line=reader.readLine()始终为空 URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" + "v=1.0&q=Official%20Google%20Blog"); URLConnection connection = url.openConnection(
URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
"v=1.0&q=Official%20Google%20Blog");
URLConnection connection = url.openConnection();
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
试试这个
URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
"v=1.0&q=Official%20Google%20Blog");
URLConnection connection = url.openConnection();
ByteArrayOutputStream content = new ByteArrayOutputStream();
InputStream is = connection.getInputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) >= 0)
{
content.write(buffer, 0, len);
}
byte[] finalContent = content.toByteArray();
String str = new String(finalContent, "UTF8");
System.out.print(str);
或者另一种方法是,您可以读取内容长度
标题,并一直读取,直到获得那么多数据。试试这个
URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
"v=1.0&q=Official%20Google%20Blog");
URLConnection connection = url.openConnection();
ByteArrayOutputStream content = new ByteArrayOutputStream();
InputStream is = connection.getInputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) >= 0)
{
content.write(buffer, 0, len);
}
byte[] finalContent = content.toByteArray();
String str = new String(finalContent, "UTF8");
System.out.print(str);
或者另一种方法是,您可以读取
内容长度
标题,直到获得那么多数据。如何知道它的空值,请尝试在while循环中打印行
,并检查实际发生的情况。我使用调试器进行了检查。我尝试了您的代码,并且能够在不做任何修改的情况下获取数据。我建议你在while循环中输入一条打印语句并检查。你是对的。但是还有另一个问题。代码没有获取完整的数据。如果您知道它的空值,请尝试在while循环中打印行
,并检查实际发生的情况。我使用调试器进行了检查。我尝试了您的代码,并且能够在不进行任何修改的情况下获取数据。我建议你在while循环中输入一条打印语句并检查。你是对的。但是还有一个问题,代码没有得到完整的数据,我发现logcat没有打印完整的数据。无论如何,谢谢。我发现logcat没有打印全部数据。无论如何,谢谢你。