iHacks4iPhone
iHacks4iPhone

Reputation: 51

Fetching Picture info

I'm trying to fetch picture's info using this code. I'm having trouble getting the link of the picture.

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask task = new DownloadTask();
        task.execute("..."); //I'm not sharing the link here

    }

    public class DownloadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);

                urlConnection = (HttpURLConnection) url.openConnection();

                InputStream in = urlConnection.getInputStream();

                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while (data != -1) {

                    char current = (char) data;

                    result += current;

                    data = reader.read();

                }

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {

//Problem must be here :

                JSONObject jsonObject = new JSONObject(result);
                String data = jsonObject.getString("data");
                String images = jsonObject.getString("images");
                String standard_resolution = jsonObject.getString("standard_resolution");

                Log.i("Content", data);

                JSONArray arr = new JSONArray(standard_resolution);

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

                    JSONObject jsonPart = arr.getJSONObject(i);

                    Log.i("description", jsonPart.getString("url"));
                    Log.i("description", jsonPart.getString("description"));

//till here

                }


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



        }
    }


}

This is the webpage response.

{
    "data": {
        "type": "image",
        "users_in_photo": [{
            "user": {
                "username": "kevin",
                "full_name": "Kevin S",
                "id": "3",
                "profile_picture": "..."
            },
            "position": {
                "x": 0.315,
                "y": 0.9111
            }
        }],
        "filter": "Walden",
        "tags": [],
        "comments": {
            "count": 2
        },
        "caption": null,
        "likes": {
            "count": 1
        },
        "link": "http://instagr.am/p/D/",
        "user": {
            "username": "kevin",
            "full_name": "Kevin S",
            "profile_picture": "...",
            "id": "3"
        },
        "created_time": "1279340983",
        "images": {
            "low_resolution": {
                "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg",
                "width": 306,
                "height": 306
            },
            "thumbnail": {
                "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg",
                "width": 150,
                "height": 150
            },
            "standard_resolution": {
                "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg",
                "width": 612,
                "height": 612
            }
        },
        "id": "3",
        "location": null
    }
}

Upvotes: 1

Views: 60

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191864

There are no arrays in the path of the data you want. Just objects

String resolution = "standard_resolution";
JSONObject jsonObject = new JSONObject(result);
JSONObject images = jsonObject.getJSONObject("data").getJSONObject("images");
String url  = images.getJSONObject(resolution).getString("url");

Upvotes: 1

Harsh Kapoor
Harsh Kapoor

Reputation: 214

To get the images form the your JSON you need to understand how many JSONObject and JSONArray available in your josn result and keep follow the tree structure of your json result when you follow your json result structure you will get the images

Upvotes: 0

Related Questions