Mario Weidler
Mario Weidler

Reputation: 39

How to implement a Webservice Connection from AsyncTask in Android?

I've got a Problem.

I'm working on this since hours without result:

    public void getData(){

    new Task().execute();


}

class Task extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... noargs) {
        return null;
    }

    protected void onPostExecute(String result){

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.omdbapi.com/?t=True%20Grit&y=1969");
        try {
            HttpResponse response = client.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            StringBuffer zeile = new StringBuffer("");

            while((line = rd.readLine()) != null){
                zeile.append(line);
            }

            String res = zeile.toString();

            Toast toast = Toast.makeText(getApplicationContext(), res, Toast.LENGTH_SHORT);
            toast.show();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Everytime (the getDate() is called by a button) I get an exception and on my phone it says that "Unfortunately the app was finished". Do yout know where my mistake is?

The Exception is a: NetworkOnMainThreadException, how can I solve it?

Thank U so much!

Upvotes: 1

Views: 141

Answers (2)

Egor
Egor

Reputation: 40203

You should move all network related code to doInBackground() and process the result of the network call inside onPostExecute(). Here's how your code should look like:

class Task extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... noargs) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.omdbapi.com/?t=True%20Grit&y=1969");
        try {
            HttpResponse response = client.execute(request);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            StringBuffer zeile = new StringBuffer("");

            while((line = rd.readLine()) != null){
                zeile.append(line);
            }

            String res = zeile.toString();
            return res;  
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    protected void onPostExecute(String result){
            Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
            toast.show();
    }

}

Upvotes: 1

laalto
laalto

Reputation: 152817

onPostExecute() and onPreExecute() of the async task run on the UI thread. Move the network code from your onPostExecute() to doInBackground().

Upvotes: 0

Related Questions