'IMDB-API - reading the response in while loop not reading more than one line
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();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
