间隔请求新的http请求时无法获得http响应

Cannot get http response when intervally requesting a new http request

我有一个名为 App 的 class,就是这个:

public class App{
    public static void main(String[] args){
        StreamingData dataStream = new StreamingData("urlString");
        dataStream.StreamHttpRequest();
    }
}

和这个 class 调用 StreamingData 有两个方法,StreamHttpRequest 每 1 秒间隔调用 httpRequest,如下所示:

public class StreamingData {
    private String url;
    private JSONObject httpResponse;
    public StreamingData(String url){
        this.url = url;
    }
    public void httpRequest() throws Exception{
        try {
            URL obj = new URL(this.url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            BufferedReader in = new BufferedReader(new 
                    InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            setHttpResponse(new JSONObject(response.toString()));
        } catch (ConnectException e){
            e.printStackTrace
        }
    }
    public void setHttpResponse(JSONObject httpResponse) {
        this.httpResponse = httpResponse;
    }
    public JSONObject getHttpResponse() {
        System.out.println(this.httpResponse.toString());
        return this.httpResponse;
    }
    public void StreamHttpRequest() {
        final long timeInterval = 1000;
        Runnable runnable = new Runnable() {
            public void run() {
                while(true){
                    try {
                        httpRequest();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

每当我从 httpRequest 方法调用 getHttpResponse 时,或者从 StreamingData class 调用几乎所有方法时,它 returns 整个 json 响应,但是当我尝试从 App class 中检索它时

// code as shown above
StreamingData netDataStream = new StreamingData("urlString");
JSONObject netDataHttpResponse = netDataStream.getHttpResponse();

它 returns Exception in thread "main" java.lang.NullPointerException 而 json 是空的。

如何获得对另一个 class(例如 App)的 json 响应?因为现在我不能在 StreamingData class.

之外使用它

非常感谢您的帮助, csymvoul

您正在使用线程。这意味着一旦 http 请求执行(异步),就会设置 httpResponse 字段值。

此处:

StreamingData netDataStream = new StreamingData("urlString");
JSONObject netDataHttpResponse = netDataStream.getHttpResponse();

您要求立即响应(当 http 响应未准备好时)。

您可以向您的 StreamingData 添加某种侦听器 class ,以便您可以在响应准备就绪时调用某些方法:

public class HttpResponseListener { 
    void onResponse(JSONObject httpResponse){...} 
}

然后你可以做类似...

StreamingData netDataStream = new StreamingData("urlString" , new HttpResponseListener());

并在设置 httpResponse 对象时调用 httpResponseListener.onResponse。

public void setHttpResponse(JSONObject httpResponse) {
    this.httpResponse = httpResponse;
    httpResponseListener.onResponse(httpResponse);
}

也就是说,如果您仍想对线程使用 Http 请求/响应。