csymvoul
csymvoul

Reputation: 707

Cannot get http response when intervally requesting a new http request

I have a class called App which is this one:

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

and this class called StreamingData that has two methods, the StreamHttpRequest that intervally calls the httpRequest every 1 second as shown below:

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();
    }

Whenever I call the getHttpResponse from the httpRequest method, or pretty much every method from the StreamingData class it returns the whole json response, but when I try to retrieve it from the App class like this

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

it returns Exception in thread "main" java.lang.NullPointerException and the json is empty.

How can I get the json response to another class (e.g. the App)? Because for now I cannot use it outside the StreamingData class.

Thank you very much for your help, csymvoul

Upvotes: 2

Views: 61

Answers (1)

Martín Zaragoza
Martín Zaragoza

Reputation: 1817

You're working with threads. That means that the httpResponse field value will be set once the http request performs (async).

here:

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

You're asking for the response immediatelly (when the http response is not ready).

You could add some kind of listener to your StreamingData class , so that you can call on some method when the response is ready:

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

Then you could do something like...

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

And call httpResponseListener.onResponse when the httpResponse object is set.

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

That is if you still want to use Http request / reponses with threads.

Upvotes: 1

Related Questions