处理错误中的API调用

时间:2018-02-12 19:04:38

标签: api processing

我正在尝试在Processing中执行基本的JSON GET操作。我一直在使用两个只需要API密钥进行身份验证的开放API。一直收到以下错误消息两次:

无法为http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&APPID=f77c39703fb6be71e2c5a96e58edc077

创建阅读器

我得到的错误信息是:

FATAL EXCEPTION: Animation Thread
Process: processing.test.jsonprocessingtutorial, PID: 26486
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.Reader.markSupported()' on a null object reference
    at processing.data.JSONTokener.<init>(Unknown Source)
    at processing.data.JSONObject.<init>(Unknown Source)
    at processing.core.PApplet.loadJSONObject(Unknown Source)
    at processing.test.jsonprocessingtutorial.JSONprocessingtutorial.loadData(JSONprocessingtutorial.java:46)
    at processing.test.jsonprocessingtutorial.JSONprocessingtutorial.setup(JSONprocessingtutorial.java:42)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PSurfaceNone.callDraw(Unknown Source)
    at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)

API Key和GET调用已经过双重检查。

The code in Processing is the following: 

String API_Key = "f77c39703fb6be71e2c5a96e58edc077";
String url = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139";
//specific url with coordinates for specific city 

void setup() {
  size(480, 360);
  loadData();
  }

void loadData(){
  JSONObject json = loadJSONObject(url+"&APPID="+API_Key);
  JSONObject windData = json.getJSONObject("wind");
  float speed = windData.getFloat("speed");
  println (speed); 
  }

JSON响应如下: 试图获得&#34; Speed&#34;出.. ..

{
"coord": {
"lon": 139,
"lat": 35
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 278.455,
"pressure": 1017.64,
"humidity": 100,
"temp_min": 278.455,
"temp_max": 278.455,
"sea_level": 1027.18,
"grnd_level": 1017.64
},
"wind": {
"speed": 11.86,
"deg": 272.003
},
"clouds": {
"all": 0
},
"dt": 1518460865,
"sys": {
"message": 0.0133,
"country": "JP",
"sunrise": 1518384771,
"sunset": 1518423842
},
"id": 1851632,
"name": "Shuzenji",
"cod": 200
}

请咨询!谢谢..

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案..

解决方案是使用“Android - 草图权限”选项卡下的“INTERNET权限”..

亲切的问候,

尼尔斯