1

我正在做一个项目,我需要调用一个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());
        }
    }

调用任务。

这些是日志的结果

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 方法返回..这是不正常的..:有人可以帮助我吗?

4

0 回答 0