从网页返回字符串 - android失败

时间:2013-11-22 08:23:36

标签: android string

我正在尝试使用以下代码从网站上检索字符串:

// 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();
}

使用以下代码从Onclick调用getPage():

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)

有人可以帮我检索这个字符串并在textview中设置值吗?

感谢;

安迪

2 个答案:

答案 0 :(得分:1)

Android不允许在主线程上进行网络调用。您需要使用Aysnc Task。有关如何使用异步任务

的更多详细信息,请参阅以下链接

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

答案 1 :(得分:0)

使用AsyncTask实现与网络相关的任务