-1

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

andy
  • 391
  • 13
  • 33

2 Answers2

1

Android doesn't allow network calls on the main thread. You need to use Aysnc Task. Refer to following link for more details on how to use Async Task

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

Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15
  • Thanks for the link. I have looked at async task but it states on that blog that the task can only be run once. I would like to be able to run the task several times if required should the user click the image button more than once. How can I manage that? – andy Nov 22 '13 at 13:28
  • So each time you want to run async task create new instance of it and call execute method. – Prashant Thakkar Nov 22 '13 at 14:01
0

use AsynTask to achieve network related tasks

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37