HttpUrlConnection中找不到文件异常

时间:2015-12-17 15:32:39

标签: android

我想在 ImageView Android中显示图片。图片的网址为http://static.die.net/earth/mercator/1600.jpg
但是在使用 HttpUrlConnection 的android中, url无法访问。它给了我一个 FileNotFoundException 。 该网址可在 Chrome 邮递员中成功访问。

  

代码

class GetData extends AsyncTask <String,Void,String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String result = "";
            HttpURLConnection conn = null;
            try {
                URL url = new URL("http://static.die.net/earth/mercator/1600.jpg");
                conn = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(conn.getInputStream());
                if (in != null) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    String line = "";

                    while ((line = bufferedReader.readLine()) != null)
                        result += line;
                }
                in.close();
                return result;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                if(conn!=null)
                conn.disconnect();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }

}


我不知道为什么它在HttpUrlConnection中给出了Exception。

  

栈跟踪

12-17 21:07:15.752: W/System.err(2129): java.io.FileNotFoundException: http://static.die.net/earth/mercator/1600.jpg
12-17 21:07:15.752: W/System.err(2129):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
12-17 21:07:15.752: W/System.err(2129):     at com.example.test.MainActivity$GetData.doInBackground(MainActivity.java:80)
12-17 21:07:15.752: W/System.err(2129):     at com.example.test.MainActivity$GetData.doInBackground(MainActivity.java:1)
12-17 21:07:15.752: W/System.err(2129):     at android.os.AsyncTask$2.call(AsyncTask.java:292)
12-17 21:07:15.752: W/System.err(2129):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-17 21:07:15.752: W/System.err(2129):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-17 21:07:15.752: W/System.err(2129):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-17 21:07:15.752: W/System.err(2129):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-17 21:07:15.752: W/System.err(2129):     at java.lang.Thread.run(Thread.java:818)

我也试过了。

conn.setInstanceFollowRedirects(false);
conn.setDoOutput(false);

但没有运气。 如果有人遇到过这个问题,请告诉我。 感谢。

0 个答案:

没有答案