2013-11-22 12 views
-1

我嘗試使用下面的代碼以檢索從一個網站的字符串:返回字符串從網頁 - Android的失敗

// webgrabber starts here 
public String getPage() { 
    // lets format the icaos the user is interested in 
    icaos = etICAO1.getText().toString() + " " 
      + etICAO2.getText().toString() + " " 
      + etICAO3.getText().toString() + " " 
      + etICAO4.getText().toString() + " " 
      + etICAO5.getText().toString() + " " 
      + etICAO6.getText().toString() + " " 
      + etICAO7.getText().toString() + " " 
      + etICAO8.getText().toString() + " "; 

    //now lets format the webaddress 
    finalWebAddress = websitePart1 + icaos + websitePart2; 
    //try to get the data 
    Toast.makeText(getApplicationContext(), 
        "Collecting data from " + finalWebAddress, Toast.LENGTH_LONG).show(); 
    try { 
     HttpURLConnection con = (HttpURLConnection) new URL(finalWebAddress) 
       .openConnection(); 
     con.connect(); 

     if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      return inputStreamToString(con.getInputStream()); 
     } else { 
      Toast.makeText(getApplicationContext(), 
         "No Internet connection detected. Please fix your connection and try again.", Toast.LENGTH_LONG).show(); 
      return null; 
     } 
    } catch (IOException ex) { 
     return null; 
    } 
} 

private String inputStreamToString(InputStream in) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(in)); 
    StringBuilder stringBuilder = new StringBuilder(); 
    String line = null; 

    while ((line = bufferedReader.readLine()) != null) { 
     stringBuilder.append(line + "\n"); 
    } 

    bufferedReader.close(); 
    //set the TV to the weather we returned 
    return stringBuilder.toString(); 
} 

的GETPAGE()從一個onclick調用此代碼:

if (v == ibGrabber) {   
    getPage(); 
    tvWeather.setText(getPage()); 
} 

當我點擊按鈕,我得到的來自logcat的以下內容:

11-22 08:18:51.780: E/AndroidRuntime(11637): FATAL EXCEPTION: main 
11-22 08:18:51.780: E/AndroidRuntime(11637): android.os.NetworkOnMainThreadException 
11-22 08:18:51.780: E/AndroidRuntime(11637): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 
11-22 08:18:51.780: E/AndroidRuntime(11637): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 

有人可以幫我找回這個海峽並在我的文本視圖中設置值?

感謝;

安迪

回答

1

的Android不允許在主線程中調用網絡。您需要使用Aysnc任務。請參考以下鏈接瞭解如何使用異步任務

http://androidpartaker.wordpress.com/2010/08/01/android-async-task/

+0

感謝您的鏈接瞭解詳情。我查看了異步任務,但它在該博客上聲明該任務只能運行一次。如果用戶需要多次單擊圖像按鈕,我希望能夠多次運行該任務。我該如何管理? – andy

+0

所以每次你想運行異步任務創建它的新實例並調用execute方法。 –

0

使用AsynTask實現網絡相關的任務