AsyncTask - 未调用onPostExecute

时间:2012-08-28 12:06:23

标签: android http android-asynctask httpclient

我正在处理一个项目,我需要调用一个AsyncTask,但不会调用onPostExecute方法。

这是我的班级:

public class WebService extends AsyncTask<String, String, String> {
    private ArrayList<SimpleObserver> listeners;
    private int responseCode;
    private String message;
    private String response;
    private String URL;

    public WebService() {
        listeners = new ArrayList<SimpleObserver>();
    }

    public void addListener(SimpleObserver obs) {
        listeners.add(obs);
    }

    public void removeListener(SimpleObserver obs) {
        listeners.remove(obs);
    }

    public void notifyListener(String s) {
        for (SimpleObserver listener : listeners)
            listener.onChange(s);
    }

    public String getResponse() {
        return response;
    }

    public String getErrorMessage() {
        return message;
    }

    public int getResponseCode() {
        return responseCode;
    }

    @Override
    protected void onPreExecute() {
        //notifyListener("A calcular");
    }

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            HttpParams my_httpParams = new BasicHttpParams(); 
            final String proxyHost = android.net.Proxy.getDefaultHost();
            final int proxyPort = android.net.Proxy.getDefaultPort();
            if(proxyPort != -1)
            {
              my_httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));            
            }

            DefaultHttpClient client = new DefaultHttpClient(my_httpParams);
            HttpGet httpGet = new HttpGet(url);
            Log.d("URL serviço HttpGet", url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

                Log.d("RESPOSTA do web service", response);

            } catch (Exception e) {
                e.printStackTrace();
                response = e.getMessage();
                Log.e("ERRO de respota", e.getMessage());
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.d("onPostExecute Serviço", result);
        notifyListener(result);
    }
}

我创建了这个方法:

public void executeService(String param) {
        try {
            Log.d("Entrar", "no serviço");
            s.execute(new String [] {URL+param});
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("Erro ao aceder ao web service", e.getMessage());
        }
    }

调用任务。

这些是Log

的结果
08-28 17:47:21.936: D/URL serviço HttpGet(2055): http://192.168.56.1:8080/pt.Agile21.Acerola.WebService/rest/acerola?id=g;ana@eu.com
08-28 17:47:22.456: D/RESPOSTA do web service(2055): ana;ana@eu.com;pass;0
08-28 17:47:22.456: D/RESPOSTA do web service(2055): ana;ana@eu.com;pass;0

如您所见,我拥有doInBackground()的所有结果。 :S

有人可以帮我理解哪个是问题?

我现在看到的东西正在查找日志文件..我的onPostExeute方法在我故意完成我的应用程序时返回..这不正常..:S有人可以帮助我吗?

0 个答案:

没有答案