2013-08-20 213 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; 
}