0

I am trying to fetch details from the JSON with URL - https://thingspeak.com/channels/133098/field/1.json

But it always return null value. Please help to find the proper code to fetch the value from the respective cloud into my Android application.

JSON file

{
"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"
}]
}

My Android Code

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();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

3 Answers3

2

Change

for (int i = 0; i < response.length(); i++)

to

for (int i = 0; i < jsonArray.length(); i++)
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
Sagar Jogadia
  • 1,330
  • 1
  • 16
  • 25
0

This isn't how you download JSON from a url

JSONObject resultJsonObject = new JSONObject(URL);

Unless URL is actually a JSON string, that will throw a JSONException because http://bla.bla is not JSON.

Your question is tagged with Volley, so make a JsonObjectRequest.

Then you can loop over response.getJSONArray("feeds").length() in the onResponse

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You can read the url into a stringbuffer and then do the processing accordingly,

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());

}
}

once you have the JSONObject you can parse and get what you require accordingly.

Rishal
  • 1,480
  • 1
  • 11
  • 19
  • `org.json.simple` isn't needed in Android code. `JSONObject` is built into the SDK. This code would throw an exception on an Android device if called on its own – OneCricketeer Aug 09 '16 at 16:11
  • Dont have much of handson on android coding. once we have string we can use that accordingly by passing it to JSONObject(String json) which is supported by android. – Rishal Aug 09 '16 at 16:16