0

I'am using JSON Simple (https://code.google.com/p/json-simple/downloads/list).

But I have a problem with this.

I'm creating something like this:

  public String showMe() {
        JSONParser parser = new JSONParser();

        try {
            URL oracle = new URL("https://danepubliczne.imgw.pl/api/data/synop");
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                JSONArray a = (JSONArray) parser.parse(inputLine);

                for (Object o : a) {

                    JSONObject tutorials = (JSONObject) o;

                  Long id = (Long) tutorials.get("id_stacji");
                 if (id == 12650) {
                       stacja = (String) tutorials.get("stacja");
                       temperatura = (String) tutorials.get("temperatura");
                    return stacja;
               }
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

But it's not working.

I need to get stacja and temperatura with id:11 https://danepubliczne.imgw.pl/api/data/synop API and show it in TextView (with this I have no problem). How should I do?

Please help me.

simo-r
  • 733
  • 1
  • 9
  • 13
Chrisss
  • 87
  • 1
  • 10
  • Can you post where is the problem? What's the compilation output? – simo-r Jan 14 '18 at 13:15
  • id_stacji is not a long, it is a String. So you should do `Long id = Long.parseLong(tutorials.get("id_stacji"));` – joao86 Jan 14 '18 at 13:15
  • @joao86 thx, but not working... Logcat shows: "FATAL EXCEPTION: main Process: pl....., PID: 4764 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pl..../pl.....pogoda}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference" – Chrisss Jan 14 '18 at 13:45
  • @Chrisss please put the complete stack info from the Logcat in your question – joao86 Jan 14 '18 at 14:14
  • I'm sorry! It's working @joa86, that's my fault in another place in my application. Thank You! – Chrisss Jan 14 '18 at 14:28
  • cool @Chriss, I will add it as the answer. – joao86 Jan 15 '18 at 11:51
  • Android has its own JSON library. You should use it instead – OneCricketeer Jan 15 '18 at 11:56

1 Answers1

0

id_stacji is not a long, it is a String.

So you should do

Long id = Long.parseLong(tutorials.get("id_stacji"));
joao86
  • 2,056
  • 1
  • 21
  • 23