2015-07-19 64 views
0

当我测试我的应用程序在模拟器(android 5.0),它运行成功,并得到我想要的图片和getRequestCode是200.但是当我运行它在我的银河S3和摩托xt889,这个错误occoured.and getRequestCode是404.为什么这个错误liborce.net.http.httpurlconnectionimpl.getinputstream发生在android中?

public class DownloadService { 
private static String TAG = "DownloadService"; 
public static final int IO_BUFFER_SIZE = 8 * 1024; 
private static final String CACHE_FILENAME_PREFIX = "cache_"; 
private static ExecutorService SINGLE_TASK_EXECUTOR = null; 
private static ExecutorService LIMITED_TASK_EXECUTOR = null; 
private static final ExecutorService FULL_TASK_EXECUTOR = null; 
private static final ExecutorService DEFAULT_TASK_EXECUTOR; 
private Pattern p; 
private Matcher m; 
private static Object lock = new Object(); 
static { 
    // SINGLE_TASK_EXECUTOR = (ExecutorService) 
    // Executors.newSingleThreadExecutor(); 
    LIMITED_TASK_EXECUTOR = (ExecutorService) Executors 
      .newFixedThreadPool(1); 
    // FULL_TASK_EXECUTOR = (ExecutorService) 
    // Executors.newCachedThreadPool(); 
    DEFAULT_TASK_EXECUTOR = LIMITED_TASK_EXECUTOR; 
}; 

DownloadStateListener listener; 

private String downloadPath; 


private List<String> listURL; 
// 下载个数 
private int size = 0; 

private Context context; 


public interface DownloadStateListener { 
    public void onFinish(); 

    public void onFailed(); 
} 

public DownloadService(String downloadPath, List<String> listURL, 
     DownloadStateListener listener, Context context) { 
    this.context = context; 
    this.downloadPath = downloadPath; 
    this.listURL = listURL; 
    this.listener = listener; 
    p = Pattern.compile(".*com/(.*)"); 
} 


public void setDefaultExecutor() { 

} 


public void startDownload() { 

    File downloadDirectory = new File(downloadPath); 
    if (!downloadDirectory.exists()) { 
     downloadDirectory.mkdirs(); 
    } 

    for (final String url : listURL) { 

     try { 

      DEFAULT_TASK_EXECUTOR.execute(new Runnable() { 

       @Override 
       public void run() { 
        downloadBitmap(url); 
       } 
      }); 
     } catch (RejectedExecutionException e) { 
      e.printStackTrace(); 
      Log.e(TAG, "thread pool rejected error"); 
      listener.onFailed(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      listener.onFailed(); 
     } 

    } 

} 


private void downloadBitmap(String urlString) { 
    Matcher m = p.matcher(urlString); 
    OutputStream fos=null; 
    m.find(); 

    HttpURLConnection urlConnection = null; 
    BufferedOutputStream out = null; 

    try { 
     fos = context.openFileOutput(m.group(1), Context.MODE_PRIVATE); 

     final URL url = new URL(urlString); 

     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.getResponseCode(); 
     Log.d("dfdf", urlConnection.getResponseCode()+""); 


     final InputStream in = new BufferedInputStream(
       urlConnection.getInputStream(), IO_BUFFER_SIZE); 
     out = new BufferedOutputStream(fos, IO_BUFFER_SIZE); 

     int b; 
     while ((b = in.read()) != -1) { 
      out.write(b); 
     } 

     statDownloadNum(); 

    } catch (final IOException e) { 

     Log.e(TAG, "download " + urlString + " error"); 
     e.printStackTrace(); 
     listener.onFailed(); 

    } finally { 
     if (urlConnection != null) { 
      urlConnection.disconnect(); 
     } 
     if (out != null) { 
      try { 
       out.close(); 
       fos.close(); 
      } catch (final IOException e) { 
       Log.e(TAG, "Error in downloadBitmap - " + e); 
      } 
     } 
    } 

} 




private void statDownloadNum() { 
    synchronized (lock) { 
     size++; 
     if (size == listURL.size()) { 
      Log.d(TAG, "download finished total " + size); 

      DEFAULT_TASK_EXECUTOR.shutdownNow(); 

      listener.onFinish(); 
     } 
    } 
} 

}

误差

java.io.FileNotFoundException: http://dnight-math.stor.sinaapp.com/理综1_img030.jpg 
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186) 
at util.DownloadService.downloadBitmap(DownloadService.java:159) 
at util.DownloadService.access$0(DownloadService.java:136) 
at util.DownloadService$1.run(DownloadService.java:114) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:841) 

当我使用真正的手机为什么会发生这个错误?

回答

0

发生这种情况是因为服务器返回HTTP.4xx或HTTP.5xx错误。如果你想获得错误页面,你应该使用HttpURLConnection.getErrorStream()方法。 类似这样的:

InputStream inputStream = null; 
try { 
    inputStream = urlConnection.getInputStream(); 
} catch(FileNotFoundException e) { 
    inputStream = urlConnection.getErrorStream(); 
} 
相关问题