如何实现从AsyncTask的Andr​​oid中一个Webservice的连接? [英] How to implement a Webservice Connection from AsyncTask in Android?

查看:123
本文介绍了如何实现从AsyncTask的Andr​​oid中一个Webservice的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。

我在做这个,因为小时无果而终:

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

}

每次(在GETDATE()是由一个按钮调)我得到一个例外,我的手机就可以说:不幸的是,应用程序是完成。做YOUT知道我的错误是什么?

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?

例外情况是:NetworkOnMainThreadException,我怎么能解决这个问题。

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

感谢ü这么多!

推荐答案

您应该将所有与网络相关的code到 doInBackground()和处理的结果在 onPostExecute网络呼叫()。这是你的code应该怎么是这样的:

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

}

这篇关于如何实现从AsyncTask的Andr​​oid中一个Webservice的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆