onPostExecute没有被调用

时间:2012-08-27 08:31:55

标签: android android-asynctask

我有一个Activity(RecipiesActivity),它通过单击我的应用程序的Main Activitiy的“Next”按钮打开。

在RecipiesActivity onCreate中,我想在AsyncTask中调用webservice。现在,既然我还没有实现web服务,我只是调用了一点点提供的web服务(但这与我的问题无关)。

问题是虽然调用了webservice并且没有抛出异常,但结果是从doInBackground返回,但是我的onPostExecute没有被调用。我做了一些研究,我发现了下面列出的可能原因 - 但似乎没有我的理由:

  • onPostExecute参数与AsyncTask参数不匹配 - 在我的情况下匹配
  • android中的一个错误,它不允许在UI线程中执行AsyncTask。通过使用Class.forName(“android.os.AsyncTask”)来支持它 - 我试过这个并没有任何区别
  • AsyncTask不是从UI线程启动的 - 在我的情况下我认为它是
  • doInBackground不会返回 - 就我的情况而言,我已经使用调试器逐步完成了
  • 在onPostExecute之前没有使用@Override - 在我的情况下我使用的是@Override

代码如下:

public class RecipiesActivity extends Activity {

    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recipies); 
        tv = (TextView) findViewById(R.id.Response);

        //Start the WS call in an AsyncTask 
        new CallWS().doInBackground("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");          
    }

    private class CallWS extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params) {

            String result = null;
            HttpClient httpClient = new DefaultHttpClient(); 
            HttpContext localContext = new BasicHttpContext(); 
            HttpGet get_request = new HttpGet(params[0]); 
            try
            {
                HttpResponse httpResponse = httpClient.execute(get_request, localContext);
                //int responseCode = httpResponse.getStatusLine().getStatusCode();
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null)
                {
                    InputStream istream = entity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(istream));

                    try
                    {
                        result = br.readLine();
                    }
                    catch (Exception e)
                    {
                        //TODO Handle the IOException that the readline may throw 
                    }
                    finally
                    {
                        istream.close();
                    }
                }

            }catch (Exception e)
            {
                //TODO Handle the IOException and the ClientProtocolException that the 
                // httpClient.execute can throw
            }
            finally
            {
                httpClient.getConnectionManager().shutdown();
            }
            return result;
        } 

        @Override
        protected void onPostExecute(String result)
        {
            if (result == null)
            {
                tv.setText("The result is NULL");
            }
            else
            {
                tv.setText(result);
            }
        }       
    }
}

你的帮助很有用,

谢谢

3 个答案:

答案 0 :(得分:5)

而不是doInBackground(),只需调用AsyncTask的execute()方法即可。调用doInBackground()执行它在锡上所说的内容:调用doInBackground()(在同一个线程中)并返回。它不会为您致电onPostExecute()execute()将启动后台线程,在后台线程上调用doInBackground(),然后在UI线程上将doInBackground()的结果发布到onPostExecute()

答案 1 :(得分:3)

尝试更改此行

new CallWS().doInBackground("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");

new CallWS().execute("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");

答案 2 :(得分:1)

确保你的onPostExecute函数有@Override