2013-08-20 44 views
0

我有一个非常简单的程序,用于连接到互联网并将图像作为异步任务进行检索(这实际上是一个学习步骤,使其能够使用XML拉)。但是,我有两件有趣的事情发生。当我在手机上运行应用程序(Samsung Galaxy 2)时,异步代码被注释掉了,我得到一个白色屏幕,连接错误显示为他们应该,并且一切都很好(除非不连接)。当我尝试运行异步代码时,我的背景停留在手机上,图标消失,并且出现应用程序停止工作的错误。我究竟做错了什么?Android网络连接崩溃

代码的非同步:

private class BackgroundTask extends AsyncTask 
<String, Void, Bitmap> { 
    protected Bitmap doInBackground(String... url){ 
     // download an image 
     Bitmap bitmap = DownloadImage(url[0]); 
     return bitmap; 
    } 

protected void onPostExecute(Bitmap bitmap) { 
    ImageView img = (ImageView) findViewById(R.id.img); 
    img.setImageBitmap(bitmap); 
} 

} 

代码调用它:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new BackgroundTask().execute("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"); 

} 

代码的东西它调用:

private InputStream OpenHttpConnection(String urlString) 
throws IOException 
{ 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 
    try{ 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("Get"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK){ 
      in = httpConn.getInputStream(); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw new IOException("Error connecting"); 
    } 
    return in; 
} 

private Bitmap DownloadImage(String URL) 
{ 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(URL); 
     bitmap = BitmapFactory.decodeStream(in); 
     in.close(); 
    } catch (IOException e1) { 
     Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 

     e1.printStackTrace(); 
    } 
    return bitmap; 
} 

的logcat:

08-20 09:13:27.294: E/AndroidRuntime(11130): at com.example.networking.MainActivity$BackgroundTask.doInBackground(MainActivity.java:1) 
08-20 09:13:27.294: E/AndroidRuntime(11130): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
08-20 09:13:27.294: E/AndroidRuntime(11130): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
08-20 09:13:27.294: E/AndroidRuntime(11130): ... 5 more 
08-20 09:13:50.469: V/InputMethodManager(11130): ABORT input: no handler for view! 
+0

是BackgroundTask一个内部类吗? – Nizam

+0

是的,它是MainActivity的一个内部类 –

回答

0

希望以下代码将起作用。也不要忘记设置互联网权限。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ImageView img = (ImageView) findViewById(R.id.img); 
    new BackgroundTask().execute("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"); 

} 
private class BackgroundTask extends AsyncTask 
<String, Void, Bitmap> { 
    protected Bitmap doInBackground(String... url){ 
     // download an image 
     Bitmap bitmap = DownloadImage(url[0]); 
     return bitmap; 
    } 

protected void onPostExecute(Bitmap bitmap) { 
    img.setImageBitmap(bitmap); 
} 

} 
private Bitmap DownloadImage(String urlString) 
{ 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
URL url = new URL(urlString); 
     in=(InputStream) url.getContent(); 
    bitmap =BitmapFactory.decodeStream(in); 
     in.close(); 
    } catch (IOException e1) { 
     Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 

     e1.printStackTrace(); 
    } 
    return bitmap; 
}