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

csymvoul:

我有一个叫的班级App

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

并调用StreamingData此类,该类具有两个方法,该类每隔1秒StreamHttpRequest间隔调用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();
    }

每当我getHttpResponsehttpRequest方法中调用,或者从StreamingData类中调用几乎所有方法时,它都会返回整个json响应,但是当我尝试App像这样类中检索它时

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

它返回Exception in thread "main" java.lang.NullPointerException并且json为空。

如何获得对另一个类(例如App的json响应因为现在我不能在StreamingData课堂上使用它

非常感谢您的帮助,csymvoul

马丁·萨拉戈萨(Martin Zaragoza):

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

这里:

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

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

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

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请求/响应。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章