Stillie
Stillie

Reputation: 2676

Using the urls from parsed info from a JSON file to display the images in the URLs

Im using a GET Request with Volley:

HTTPRequest.java

public void get(String url) {

    // Request a string response from the provided URL.
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url,
            getReponse(), createMyReqErrorListener()) {

        @Override
        protected void deliverResponse(JSONObject response) {
            Log.d(TAG, "deliverResponse= " + response.toString());
            super.deliverResponse(response);
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put(ACCEPT, ACCEPT_JSON);
            headers.put(AUTH_ID, AUTH_ID_VALUE);
            headers.put(PLATFORM, PLATFORM_VALUE);
            headers.put(CID, "");
            System.out.println(headers.toString());
            return headers;
        }
    };

    // Add the request to the RequestQueue.
    queue.add(request);
}

to grab a JSON from a url below is an example of the JSON (Dummy content):

JSON File on remote server

{
 "Errors": null,
 "Session": {
 "SessionId": ""
},
 "someUrl": "http://www.someurl.com",
 "BlockPromotions": {
 "@CategoryId": "1",
 "Type": "PAGE",
 "Items": {
  "Item": [
    {
      "@Id": "2",
      "DisplayImage": "http://imageurl/something.png",
      "DisplayOrder": "1"
    },
    {
      "@Id": "3",
      "DisplayImage": "http://imageurl/something_else.png",
      "DisplayOrder": "2"
    }
  ]
}
}
}

The code I am using to parse the JSON is below:

Parser.java

public String parseResponse(JSONObject response) {
    try {
        JSONObject objectBlockPromo = response.getJSONObject("BlockPromotions");
        JSONObject objectItems = objectBlockPromo.getJSONObject("Items");
        JSONArray arrayItem = objectItems.getJSONArray("Item");
        for (int i = 0; i < arrayItem.length(); i++) {
            JSONObject item = arrayItem.getJSONObject(i);
            ImageURL = item.getString("DisplayImage");
            System.out.println(ImageURL);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return ImageURL;
}

I managed to grab all the url's individually, my problem is handing those URL's back over to the HomeScreen, to display the images?

How would I do this?

I have tried using the URL as a String, I have also tried (but I think I did it incorrectly) using ImageRequest from Volley to grab the URL.

Thanks

Upvotes: 2

Views: 257

Answers (2)

Anexon
Anexon

Reputation: 133

In some point you are going to need cache the photos that you download, so what I recommend is try to use some of the most commonly used ImageLoader libraries.

Most popular, recommended by Google: Picasso or Glide.

After importing some of those utils into your project, in some point you just have to call a function like mImageLoader(mPhotoUrl) and attach it to your ImageView or whatever, and finally reach the goal of your question ;-).

I know it's not a very specific answer but I would have desired that someone told me this when I was struggling with the same things.

Upvotes: 0

Carsten
Carsten

Reputation: 806

You need to open a connection to the URL, download the image and then display it.

                String name = "yourdesiredfilename";
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                OutputStream output = openFileOutput(name, MODE_PRIVATE);
                IOUtils.copy(input, output);
                IOUtils.closeQuietly(input);
                IOUtils.closeQuietly(output);

(IOUtils just because I am lazy ;))
Then you have the file locally and can pass it to an ImageView or whatever you have.

                Drawable d = Drawable.createFromPath(pathName);
                ImageView yourView;
                yourView.setImageDrawable(d);

Upvotes: 1

Related Questions