IMDB-API - 在 while 循环中读取响应不读取超过一行
我正在从 Java 代码调用 IMDB-Api。它成功连接到 IMDB api 并读取数据,但由于某种原因,它读取的内容不超过一行。我在“aline”变量中看到了整个响应。但是 while 循环只执行一次..我想知道为什么?
import javax.accessibility.AccessibleTable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.SoftReference;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import org.json.JSONObject;
public class APExamPractice {
public static void main(String args[]) {
HttpURLConnection aconnection = null;
String urltext = "https://imdb-api.com/API/AdvancedSearch/keyxxx/?genres=action,adventure";
try {
URL aurl = new URL(urltext);
aconnection = (HttpURLConnection)aurl.openConnection();
aconnection.setRequestMethod("GET");
aconnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
aconnection.setDoInput(true);
InputStream astream = aconnection.getInputStream();
BufferedReader areader = new BufferedReader(new InputStreamReader(astream));
StringBuilder aresponse = new StringBuilder();
String aline = null;
while((aline = areader.readLine())!=null){
//I see the entire response in 'aline' variable. But the while gets executed only
once.. I wonder why?
aresponse.append(aline);
aresponse.append("/r");
}
areader.close();
String aresult = aresponse.toString();
System.out.println(aresult);
JSONObject aobject = new JSONObject(aresult);
String ratings= aobject.getString("imDb");
}catch (MalformedURLException e){
System.out.println("Malformed exception");
}catch (IOException e) {
System.out.println("IO exception");
}
catch(Exception e){
e.printStackTrace();
}
}
I am calling the IMDB-Api from Java code. It makes successful connection to the IMDB api and also reads but for some reason it doesn't read more than one line. I see the entire response in 'aline' variable. But the while loop gets executed only once.. I wonder why?
import javax.accessibility.AccessibleTable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.SoftReference;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import org.json.JSONObject;
public class APExamPractice {
public static void main(String args[]) {
HttpURLConnection aconnection = null;
String urltext = "https://imdb-api.com/API/AdvancedSearch/keyxxx/?genres=action,adventure";
try {
URL aurl = new URL(urltext);
aconnection = (HttpURLConnection)aurl.openConnection();
aconnection.setRequestMethod("GET");
aconnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
aconnection.setDoInput(true);
InputStream astream = aconnection.getInputStream();
BufferedReader areader = new BufferedReader(new InputStreamReader(astream));
StringBuilder aresponse = new StringBuilder();
String aline = null;
while((aline = areader.readLine())!=null){
//I see the entire response in 'aline' variable. But the while gets executed only
once.. I wonder why?
aresponse.append(aline);
aresponse.append("/r");
}
areader.close();
String aresult = aresponse.toString();
System.out.println(aresult);
JSONObject aobject = new JSONObject(aresult);
String ratings= aobject.getString("imDb");
}catch (MalformedURLException e){
System.out.println("Malformed exception");
}catch (IOException e) {
System.out.println("IO exception");
}
catch(Exception e){
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论