我正在尝试使用网址 - https://thingspeak.com/channels/133098/field/1.json
从JSON中获取详细信息但它总是返回null值。请帮助找到正确的代码,以便将相应云中的值提取到我的Android应用程序中。
JSON文件
{
"channel": {
"id": 133098,
"name": "Health Monitoring System",
"latitude": "0.0",
"longitude": "0.0",
"field1": "Temp",
"field2": "Hum",
"field3": "Pressure",
"field4": "Pulse",
"field5": "Acc",
"field6": "BP Sys",
"field7": "BP Dia",
"created_at": "2016-07-11T07:06:18Z",
"updated_at": "2016-07-22T07:56:52Z",
"last_entry_id": 134
},
"feeds": [{
"created_at": "2016-07-21T12:17:17Z",
"entry_id": 35,
"field1": "93.59"
}, {
"created_at": "2016-07-21T12:18:08Z",
"entry_id": 36,
"field1": "93.59"
}, {
"created_at": "2016-07-21T12:18:59Z",
"entry_id": 37,
"field1": "93.59"
}, {
"created_at": "2016-07-22T04:49:44Z",
"entry_id": 38,
"field1": "0.00"
}, {
"created_at": "2016-07-22T04:50:41Z",
"entry_id": 39,
"field1": "95.16"
}, {
"created_at": "2016-07-22T04:51:25Z",
"entry_id": 40,
"field1": "95.16"
}, {
"created_at": "2016-07-22T04:52:17Z",
"entry_id": 41,
"field1": "95.16"
}]
}
我的Android代码
private ArrayList<Number> temp;
JSONObject resultJsonObject = new JSONObject(URL);
JSONArray jsonArray = resultJsonObject.getJSONArray("feeds");
// temp = new Number[response.length()];
try {
for (int i = 0; i < response.length(); i++) {
final JSONObject json = jsonArray.getJSONObject(i);
temp.add(Float.parseFloat(json.getString(TAG_TEMP)));
Log.d("RESPONSE", json.getString("field1"));
}
//Toast.makeText(MainActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
//Log.d("RESPONSE", s.toString());
} catch (JSONException e) {
e.printStackTrace();
}
答案 0 :(得分:2)
更改
for (int i = 0; i < response.length(); i++)
到
for (int i = 0; i < jsonArray.length(); i++)
答案 1 :(得分:0)
这不是您从网址
下载JSON的方式JSONObject resultJsonObject = new JSONObject(URL);
除非URL
实际上是一个JSON字符串,否则会抛出JSONException
,因为http://bla.bla
不是JSON。
您的问题已标记为Volley,因此请JsonObjectRequest
。
然后,您可以在response.getJSONArray("feeds").length()
onResponse
答案 2 :(得分:0)
您可以将URL读入stringbuffer,然后相应地进行处理,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
class exampleproblem {
private static final String USER_AGENT = "Mozilla/5.0";
public static void read() throws IOException, ParseException {
ArrayList<Number> temp;
String url = "https://thingspeak.com/channels/133098/field/1.json";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", USER_AGENT);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
/*Parsing using JSON simple library: START*/
JSONParser parser = new JSONParser();
JSONObject objectRead = (JSONObject) parser.parse(response.toString());
System.out.println(objectRead.get("feeds"));
/*Parsing using JSON simple library: END*/
/*Android Compatible */
JSONObject objectRead = JSONObject(response.toString());
}
}
一旦你拥有了JSONObject,你就可以解析并得到你需要的东西。