Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中从AsyncTask实现Webservice连接?_Android_Rest - Fatal编程技术网

如何在Android中从AsyncTask实现Webservice连接?

如何在Android中从AsyncTask实现Webservice连接?,android,rest,Android,Rest,我有个问题 我从几个小时开始就在做这个,但没有结果: public void getData(){ new Task().execute(); } class Task extends AsyncTask<Void, Void, String>{ @Override protected String doInBackground(Void... noargs) { return null; } protect

我有个问题

我从几个小时开始就在做这个,但没有结果:

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

}
public void getData(){
新任务().execute();
}
类任务扩展了异步任务{
@凌驾
受保护的字符串doInBackground(无效…无args){
返回null;
}
受保护的void onPostExecute(字符串结果){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet请求=新建HttpGet(“http://www.omdbapi.com/?t=True%20Grit&y=1969");
试一试{
HttpResponse response=client.execute(请求);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
StringBuffer zeile=新的StringBuffer(“”);
而((line=rd.readLine())!=null){
zeile.append(行);
}
String res=zeile.toString();
Toast Toast=Toast.makeText(getApplicationContext(),res,Toast.LENGTH\u SHORT);
toast.show();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
每次(通过按钮调用getDate()时)我都会收到一个异常,手机上会显示“不幸的是,应用程序完成了”。你知道我的错在哪里吗

异常是:NetworkOnMainThreadException,如何解决它


非常感谢你

在UI线程上运行异步任务的
onPostExecute()
onPreExecute()
。将网络代码从
onPostExecute()
移动到
doInBackground()
您应该将所有与网络相关的代码移动到
doInBackground()
,并在
onPostExecute()
内处理网络调用的结果。下面是您的代码的外观:

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

}
类任务扩展了异步任务{
@凌驾
受保护的字符串doInBackground(无效…无args){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet请求=新建HttpGet(“http://www.omdbapi.com/?t=True%20Grit&y=1969");
试一试{
HttpResponse response=client.execute(请求);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
StringBuffer zeile=新的StringBuffer(“”);
而((line=rd.readLine())!=null){
zeile.append(行);
}
String res=zeile.toString();
返回res;
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回“”;
}
受保护的void onPostExecute(字符串结果){
Toast Toast=Toast.makeText(getApplicationContext(),result,Toast.LENGTH\u SHORT);
toast.show();
}
}