IMDB-API - 在 while 循环中读取响应不读取超过一行

发布于 2025-01-10 06:03:46 字数 2128 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文