我想从ftpserver在GridView中显示图像,我得到FileNotFound Exception

时间:2017-03-23 14:46:45

标签: android gridview

我可以非常轻松地通过FileZilla访问同一个文件夹和文件 我肯定做错了什么,但是什么? 我的代码如下

    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();  
}

2 个答案:

答案 0 :(得分:2)

它无法工作的原因是因为你需要在后台线程中运行它,我希望你使用AsyncTask(Android: AsyncTask to make an HTTP GET Request?

答案 1 :(得分: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;
    }
 }

像这样使用:

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

还要确保您在AndroidManifest.xml中拥有Internet权限:

<uses-permission android:name="android.permission.INTERNET" />