0

I try to create java program that read JSON from this url, the url contain JSON array that updated every 20 seconds here is my java program that listen to the url and print the last JSON object from the JSON file:

ListenTojson.java

package com.company;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
class ListenToJson implements Runnable {

public void run() {
    long tenSeconds = 10*1000L;
    while(true) {

        try {
            JSONArray json = readJsonFromUrl("http://frozen-brook-16337.herokuapp.com/history.json");
            JSONObject jo=json.getJSONObject(json.length()-1);
            System.out.println(jo.get("data"));
            Thread.sleep(tenSeconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONArray jsonArray = new JSONArray(jsonText);
        return jsonArray;
    } finally {
        is.close();
    }
}
}

Main.java

package com.company;

public class Main {

public static void main(String[] args){
    (new Thread(new ListenToJson())).start();
 }
}

The code run as I expected. The JSON file contains coordinate that keep updated every 10 seconds. I need to create map application that read the coordinate from the JSON and then show it on the map as marker, I use JavaFx library to create the map application, when I add the code above (ListenToJson.java) the program behave differently when I use JavaFX library it does not read the latest JSON file.

here is the code

public class Controller{
 //some code
 (new Thread(new ListenToJson())).start();
 //some code
}

Controller.java

The full class that I try to update the UI using the ListenToJson() function is here https://github.com/kikirizki/mapapp/blob/master/src/main/java/com/delameta/vesselmap/Controller.java the line 526, thanks

whats wrong with my code, why the ListenToJson() function read the outdated JSON file ?

Community
  • 1
  • 1
kkr4k
  • 123
  • 1
  • 8
  • Nothing in your `ListenToJson` class updates the UI. Can you show the version of the code that updates the UI each time it reads the new JSON? – James_D May 29 '17 at 04:14
  • I have update the question and I create new function named ListenToJson and merged it with the controller.java. note :the code is not actually update the UI yet, it just print the JSON using System.out.print(), and the result show that the JSON file is outdated – kkr4k May 29 '17 at 04:51
  • Why not use a timer -> [here](https://stackoverflow.com/questions/16128423/how-to-update-the-label-box-every-2-seconds-in-java-fx)? – SedJ601 May 29 '17 at 06:10
  • I have try using timer it yields the same result – kkr4k May 29 '17 at 07:00

1 Answers1

0

I found the solution myself, the problem is the mapjfx library (I use in the code) use cache to cache the map tile and then it cause to cache the JSON file too. The solution is simply disable the cache mechanism, just diable it at line 350, change

offlineCache.setActive(true); 

to be

offlineCache.setActive(false);  
kkr4k
  • 123
  • 1
  • 8
  • note: I am the author of the mapjfx lib; when caching is activated, **every** HttpUrlConnection request is cached at the moment. I will create an issue for enhancement that certain urls or pattern may be excluded from caching. But I can't promise, when I will implement that, I am quite busy at the moment. – P.J.Meisch May 29 '17 at 17:30
  • https://www.sothawo.com/2017/06/mapjfx-1-13-0-adds-the-possibility-to-exclude-urls-from-being-cached/ – P.J.Meisch Jun 02 '17 at 21:51