1

I'm developing a RSS reader app, but I'm facing problems with the image URLs becuase they contain spaces. I tried several techniques from these forums but they all give me different errors. I tried replacing the space with %20 but I'm getting file not found exception. This is the method in which I'm fetching the image:

//String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
            //url = Uri.encode(url, ALLOWED_URI_CHARS);
            //URL urlnew = new URL(url);
            //URI uri = new URI(urlnew.getProtocol(), urlnew.getUserInfo(), urlnew.getHost(), urlnew.getPort(), urlnew.getPath(), urlnew.getQuery(), urlnew.getRef());
            //urlnew = uri.toURL();
            //url = URLEncoder.encode(url.replace(" ", "%20"), "utf-8");
            url = url.replaceAll(" ", "%20");

            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            Log.i("Code:",conn.getResponseCode()+" "+conn.getResponseMessage());
            Log.i("Error Message",conn.getErrorStream()+"");
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;

The commented commands at the top are some commands I got from other people. LogCat:

02-20 13:58:19.015: W/System.err(29109): java.io.FileNotFoundException: http://ghadinews.net/upload/new/GhadiNews%20-%20parrot%20-%20ly.jpg
02-20 13:58:19.015: W/System.err(29109):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader.getBitmap(ImageLoader.java:94)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader.access$0(ImageLoader.java:63)
02-20 13:58:19.015: W/System.err(29109):    at com.betaclass.ghadinews.ImageLoader$PhotosLoader.run(ImageLoader.java:168)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-20 13:58:19.020: W/System.err(29109):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-20 13:58:19.020: W/System.err(29109):    at java.lang.Thread.run(Thread.java:856)

If I insert the encoded URL which contains %20 into the browser the image opens normally so the links are working.

nick28
  • 67
  • 3
  • 15

3 Answers3

2

Remove both :

conn.setDoInput(true);
conn.setDoOutput(true);

Hope this should solve the problem.

Aparupa Ghoshal
  • 309
  • 2
  • 10
  • removed it still same exception :/ – nick28 Feb 20 '14 at 13:07
  • tried it, I still get the same exception. The code works perfectly if the links don't contain spaces. But the problem is that they do. – nick28 Feb 20 '14 at 13:13
  • Try this : String url = ""; url = url.replaceAll(" ", "%20"); try { url = URLEncoder.encode(url, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } – Aparupa Ghoshal Feb 20 '14 at 13:23
  • I actually merged your answer with the "PsyGik" answer to get it to work, his method doesn't work without removing the commands you told me to remove :) – nick28 Feb 20 '14 at 13:32
2
String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Related: Bad URL encoding for images

Community
  • 1
  • 1
PsyGik
  • 3,535
  • 1
  • 27
  • 44
  • 1
    Thanks a lot! it worked perfectly, although as you can see from my code above I had tried it before and it didn't work, but I removed conn.setDoInput(true); conn.setDoOutput(true); and then it worked. – nick28 Feb 20 '14 at 13:30
0

Try this technique

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

Or use something like

String uri = Uri.parse("http://...")
                .buildUpon()
                .appendQueryParameter("key", "val")
                .build().toString();
Zare Ahmer
  • 808
  • 5
  • 8