I am trying to retrive a string from a website using the following code:
// 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();
}
The getPage() is called from an Onclick with this code:
if (v == ibGrabber) {
getPage();
tvWeather.setText(getPage());
}
when I click on button i get the following from 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)
Can someone help me retrieve this string and set the value in my textview?
Thanks;
Andy