从网页返回字符串-android失败
我正在尝试使用以下代码从网站检索字符串:从网页返回字符串-android失败,android,string,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.getT
// 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)
11-22 08:18:51.780:E/AndroidRuntime(11637):致命异常:main
11-22 08:18:51.780:E/AndroidRuntime(11637):android.os.NetworkOnMainThreadException
11-22 08:18:51.780:E/AndroidRuntime(11637):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于java.net.InetAddress.getAllByName(InetAddress.java:214)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpConnection.(HttpConnection.java:70)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpConnection.(HttpConnection.java:50)
11-22 08:18:51.780:E/AndroidRuntime(11637):在libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-22 08:18:51.780:E/AndroidRuntime(11637):在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-22 08:18:51.780:E/AndroidRuntime(11637):在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)上
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-22 08:18:51.780:E/AndroidRuntime(11637):位于libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-22 08:18:51.780:E/AndroidRuntime(11637):在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
有人能帮我检索这个字符串并在我的文本视图中设置值吗
谢谢,
Andy使用AsynTask来完成与网络相关的任务Android不允许在主线程上进行网络调用。您需要使用Aysnc任务。有关如何使用异步任务的更多详细信息,请参阅以下链接
谢谢您的链接。我已经看过async任务,但它在博客上声明该任务只能运行一次。如果用户多次单击图像按钮,我希望能够在需要时多次运行该任务。我如何管理它呢?所以每次您想要运行异步任务时,都要创建它的新实例并调用execute方法。