0

I can access the same folder and file through FileZilla very easily.
Definitely I am doing something wrong, but what ? My code is as below

    try{  
    Bitmap bitmap = null;  
    InputStream in;  
    String userpass = "username:password";  
    String url = "http://veedeesoft.com/httpdocs/RoyalFoods/Products/Large/1.png";  
    HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();  
    c.setRequestMethod("GET");  
    c.setDoInput(true);
    String basicAuth = "Basic " + new String(android.util.Base64.encode(userpass.getBytes(), Base64.DEFAULT));  
    c.addRequestProperty ("Authorization", basicAuth);  
    c.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
    c.connect();  
int status = c.getResponseCode();  
                if(status >= HttpStatus.SC_BAD_REQUEST){  
                    in = c.getErrorStream();  
                }else{  
                    in = c.getInputStream();  
                }

}catch(Exception e){  
    e.printStackTrace();  
}
Dev
  • 260
  • 4
  • 18
  • 34

2 Answers2

2

The reason why it's not working is because you need to run it in a background thread, I would prefer you use AsyncTask (Android: AsyncTask to make an HTTP GET Request?)

Community
  • 1
  • 1
Chester Cobus
  • 701
  • 4
  • 12
  • Transferred code to Async Task but still getting int status = 404. – Dev Mar 23 '17 at 15:27
  • I think there is some problem in my url. String url = http://veedeesoft.com/httpdocs/RoyalFoods/Products/Large/1.png; Is this the right way of writing url while connecting ftp server ? – Dev Mar 23 '17 at 15:40
  • http://veedeesoft.com/RoyalFoods/Products/Large/1.png, this is working in the browser. I removed "httpdocs" because thats the site hosting folder (veedeesoft.com) – Chester Cobus Mar 23 '17 at 15:47
  • No, this is not working in the browser because it is secured by username and password. But works fine with Filezilla. – Dev Mar 23 '17 at 15:50
  • It does I get Amul butter image in the browser. The whole authorization code you using is not needed. As far as I know you are accessing a HTTP image URL, not an FTP. – Chester Cobus Mar 23 '17 at 15:51
  • Oh yes, it does. It means I have to remove httpdocs from my string url... – Dev Mar 23 '17 at 15:52
  • Yes and the basic authorization and change you Content Type to application/octet-stream – Chester Cobus Mar 23 '17 at 15:56
  • Removed basic Authorization also, but same 404. Definitely i am doing something wrong... – Dev Mar 23 '17 at 15:59
  • http://stackoverflow.com/questions/18210700/best-method-to-download-image-from-url-in-android. You don't even need this c.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); – Chester Cobus Mar 23 '17 at 16:02
  • Removed c.setRequestMethod("GET"); Removed Content-Type but still same 404 – Dev Mar 23 '17 at 16:07
2
public class HttpGetTask extends AsyncTask<String, Void, Void> {
    protected Void doInBackground(String... urls) {
      try{  
            Bitmap bitmap = null;  
            InputStream in;    
              HttpURLConnection c = (HttpURLConnection) new URL(urls[0]).openConnection();  
            c.setRequestMethod("GET");  
            c.setDoInput(true);

            c.connect();  
            int status = c.getResponseCode();  
            if(status >= HttpStatus.SC_BAD_REQUEST){  
                in = c.getErrorStream();  
            }else{  
                in = c.getInputStream();  
            }

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

         return null;
    }
 }

Use it like this:

new HttpGetTask().execute("http://veedeesoft.com/RoyalFoods/Products/Large/1.png");

Also make sure you have the Internet permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> 
Chester Cobus
  • 701
  • 4
  • 12