0

I am using an API that returns a String for the URL of a photo. It is in a strange format, however, and it is causing me issues downloading the images using Volley (or any other method for that matter).

My code looks like this:

imageview = (ImageView) findViewById(R.id.imageView);
        String web_url = "http:\\/\\/static.giantbomb.com\\/uploads\\/square_avatar\\/8\\/87790\\/1814630-box_ff7.png";
        String web_url2 = "http://www.finalfantasyviipc.com/images/media_cloud_big.jpg";

        ImageRequest ir = new ImageRequest(web_url2, new Response.Listener<Bitmap>() {

            @Override
            public void onResponse(Bitmap response) {
                imageview.setImageBitmap(response);
                Log.d("image was ", "set");
            }

            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "ERROR: " + error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d("ERROR", error.getMessage());
            }
        }, 0, 0, null, null);

        requestQueue.add(ir);

As you can see, the second URL works fine, but the first URL, the one with the many backward and forward slashes, will not return an image. You can copy both into your browser and they work fine, but the first one cannot be read by my Android parsing. Does anyone have any recommendations of how to get an image from the first link? Nearly everything I have tried utilizes Strings, which seems to be a core part of the problem.

Thanks for your help!

-Sil

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • It's not your responsibility to correct other people's malformed URLs. Anything you did would be just a guess. Get them to fix it. – user207421 Feb 07 '15 at 00:58
  • A true point, but the problem is, that url works fine if you paste it into your browser, it simply will not work via my Android parsing. – PGMacDesign Feb 07 '15 at 00:59
  • 1
    do a regex to replace the "\\" see : http://stackoverflow.com/questions/11012253/replacing-double-backslashes-with-single-backslash – Robert Rowntree Feb 07 '15 at 01:31

1 Answers1

3

You can copy both into your browser and they work fine, but the first one cannot be read by my Android parsing

The first URL is definitely malformed but the reason why it works in browsers is because they automatically convert backslashes to forward slashes and most web servers tend to ignore multiple consecutive forward slashes.

For example, if I enter that URL in Chrome (OSX), the URL in the address bar changes to:

http://static.giantbomb.com///uploads///square_avatar///8///87790///1814630-box_ff7.png

which seems to work fine. So, to solve your problem on Android, just do the same:

web_url = web_url.replace("\\", "/");

or even better:

web_url = web_url.replace("\\", "");

That should convert the URL to http://static.giantbomb.com/uploads/square_avatar/8/87790/1814630-box_ff7.png and therefore fix your issue.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70