0
votes

Alright so I'm attempting to pull data from JSON's lists such as this:

{"code":200,"status":"OK","data":{"timings":{"Fajr":"04:17","Sunrise":"05:30","Dhuhr":"12:20","Asr":"15:42","Sunset":"19:11","Maghrib":"19:11","Isha":"20:24","Imsak":"04:07","Midnight":"00:20"},"date":{"readable":"17 Jun 2017","timestamp":"1497652236"}}}

exclusively the specific prayers. My code to find the data is as follows:

 class SaveTheFeed extends AsyncTask<Void, Void, Void>{
    String jsonString = "";
    String result = "";
    EditText chosenCity = (EditText) findViewById(R.id.editText3);
    String city = chosenCity.getText().toString();

    @Override
    protected Void doInBackground(Void... voids) {
        DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httpPost = new HttpPost("http://api.aladhan.com/timingsByCity?city=" + city + "&country=AE&method=2");
        httpPost.setHeader("Content-type", "application/json");
        InputStream inputStream;

        try{
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) !=null){
                builder.append(line + "\n");
            }
            jsonString = builder.toString();

            JSONObject jObject = new JSONObject(jsonString);

            JSONObject data = (JSONObject) jObject.get("data");

            JSONArray jArray = (JSONArray) data.get("timings");

            outputTimings(jArray);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        TextView prayerTimes = (TextView) findViewById(R.id.prayerTimes);
        prayerTimes.setText(result);
    }

    protected void outputTimings(JSONArray jsonArray){
        String[] prayers= {"Fajr", "Sunrise", "Dhuhr", "Asr", "Sunset", "Maghrib", "Isha", "Imsak", "Midnight"};
        try{
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject cityObject =
                        jsonArray.getJSONObject(i);
                result = result +  prayers[i] + " : "+
                         cityObject.getString(prayers[i]) + "\n";
            }

I get this error that points towards line 85 (the JSONObject jObject = new JSONObject(jsonString); line)

06-17 00:24:15.973 2866-2907/azanmute.android.com.azanmute W/System.err: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 06-17 00:24:15.973 2866-2907/azanmute.android.com.azanmute W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at org.json.JSONObject.(JSONObject.java:160) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at org.json.JSONObject.(JSONObject.java:173) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at azanmute.android.com.azanmute.MainActivity$SaveTheFeed.doInBackground(MainActivity.java:85) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at azanmute.android.com.azanmute.MainActivity$SaveTheFeed.doInBackground(MainActivity.java:60) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 06-17 00:24:15.974 2866-2907/azanmute.android.com.azanmute W/System.err: at java.lang.Thread.run(Thread.java:761)

So my understanding is that Im pointing towards the data incorrectly or mixed up JSONObjects with JSONArrays. Ive already tried both this and this but still receive the same error.

1
From the exception, I'd say the problem is your String jsonString is not a valid JSON string. Can you put a debug here and provide the exact value of jsonString just before l. 85?Sir4ur0n
Just print what you are getting in HTTP response to be sure the URL sends you a valid json responseZakir
So which is line 85 of MainActivity.java? It's always useful to reduce things to a minimal reproducible example - there's a lot of irrelevant code in your question at the moment.Jon Skeet
Aside from anything else, timings isn't an array - it's another object...Jon Skeet
Your json string on the top is correct, but the error log is say that it's not correct. Then let's use the debugger to print out the jsonString before the new JSONObject(jsonString); to check it outTuyenNTA

1 Answers

1
votes

You haven't confused JSON Object and JSON Array in this case. This is definitely a JSONObject.

It's possible that you've added 'invisible' characters to your string that the JSONObject constructor doesn't like (i.e. "\n"). Try this:

JSONObject jObject = new JSONObject(jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}") + 1));

Also please note that "timings" is also a JSONObject and not a JSONArray. Example of a JSONArray, where "cars" is the JSONArray:

{"name":"John","age":30,"cars":[ "Ford", "BMW", "Fiat" ]}