AsyncTask Execute方法显示错误

时间:2015-09-22 02:57:15

标签: android android-asynctask

我正在尝试调用已扩展AsyncTask的GetMethodExample类的execute方法,但它向我显示了GetMethodExapmle未定义方法执行的错误。这是我的代码。

GetMethodExample.java:

package com.shehryar.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
public class GetMethodExample extends AsyncTask<String, Integer, String>{



@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    BufferedReader in = null;
    String data = null;
    try {
        HttpClient client = new DefaultHttpClient();

        HttpGet request = new HttpGet();
        URI website= new URI(params[0]);
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.seperator");
        while ((l = in.readLine()) != null) {
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
                return data;
            } catch (Exception e) {
                {
                    e.printStackTrace();
                }
            }
        }
    }
    return null;
}


}

// MainActivity

HttpExample.java:

package com.shehryar.httpclient;

import java.util.concurrent.ExecutionException;

import com.shehryar.httpclient.R.layout;

import android.app.Activity; 
import android.os.Bundle;
import android.widget.TextView;

public class HttpExample extends Activity {

TextView httpstuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);
    httpstuff=(TextView) findViewById(R.id.tvhttp);
    String data;
    String website="http://www.google.com";
    String extra="";
    try {
        GetMethodExample obj= new GetMethodExample();
        data=obj.execute(website).get();    //Here is the error
        httpstuff.setText(data);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        httpstuff.setText("Error:" + e.toString());
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        httpstuff.setText("Error:" + e.toString());
    }
}
}

2 个答案:

答案 0 :(得分:1)

    data=obj.execute(website).get();    //Here is the error

您正试图从AsyncTask同步获取数据。

首先,您开始执行任务:

    obj.execute(website);

然后您覆盖onPostExecute()中的AsyncTask来处理完成:

@Override
public void onPostExecute(String data) {

    httpstuff.setText(data);
}

现在不知何故,您必须向AsyncTaskActivity提供TextView引用,以便您可以在onPostExecute()更新用户界面。

阅读AsyncTask上的参考文档,了解线程如何交互以及哪些方法在哪个线程上运行。

AsyncTask | Android Developers

答案 1 :(得分:0)

感谢Kris Larson的回复,但是在重新启动eclipse后我的代码工作正常。我的代码还不错,但是日食正在制造问题