【发布时间】:2018-01-14 13:07:33
【问题描述】:
我正在使用 JSON Simple (https://code.google.com/p/json-simple/downloads/list)。
但我有一个问题。
我正在创建这样的东西:
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;
}
但它不起作用。
我需要使用 id:11 https://danepubliczne.imgw.pl/api/data/synop API 获取 stacja 和 temperatura 并在 TextView 中显示(这样我没问题)。我该怎么办?
请帮帮我。
【问题讨论】:
-
你能把问题出在哪里吗?编译输出是什么?
-
id_stacji 不是很长,它是一个字符串。所以你应该这样做
Long id = Long.parseLong(tutorials.get("id_stacji")); -
@joao86 thx,但不工作... Logcat 显示:“致命异常:主进程:pl.....,PID:4764 java.lang.RuntimeException:无法实例化活动 ComponentInfo{ pl..../pl.....pogoda}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
-
@Chrisss 请将 Logcat 中的完整堆栈信息放入您的问题中
-
对不起!它正在工作@joa86,这是我在我的应用程序的另一个地方的错。谢谢!
标签: java android json json-simple