1

所以我用方法调用这个 AsyncTask getValids,第一部分被记录但不是第二部分

     Log.v("","entered main & 0 part I");
     String[] str_validsI = new getValids().execute("main").get();
     Log.v("","entered main & 0 part II");

然而,其中的第一个 Log.v 从未打印过,并且没有调用其他任何函数

protected String[] doInBackground(String... cat){
    Log.v("We are getting a HTTP Response","1");

PreExecute 也称为

protected void onPreExecute(){
    Log.v("","We're in pre execture");
    //this is the last function that is printed
}

这是 getValids 类,doInBackground 从来没有被调用过,什么也没有返回,为什么?

有谁知道出了什么问题以及为什么不调用它?

public class getValids extends AsyncTask<String,Void,String[]> {
public List<String> retd = new ArrayList<String>();
private InputStream is = null;

//public List<String> getValids(String cat)
protected String[] doInBackground(String... cat){
    Log.v("We are getting a HTTP Response","1");
    String base_url = "http://universitytimes.ie/mobile/";
    String url = null;
    List<String> list = Arrays.asList("aaaa".split("\\s*"));
    Log.v("We are getting a HTTP Response","2");
    if (cat.equals("main")){
        url = base_url;
    }else{
        url = base_url+cat;
    }
    try {
        // default HTTPClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        Log.v("We are getting a HTTP Response","The status " + httpResponse.getStatusLine().getStatusCode());
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = "";

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        String str_valids = sb.toString();

        list = Arrays.asList(str_valids.split("\\s*,\\s*"));


    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return list.toArray(new String[list.size()]);

} //end do in bg

public void setResult(List<String> in){
    Log.v("We are getting a HTTP Response","1");
    retd = in;
}

public List<String> getResult(){
    return retd;
}

protected void onPreExecute(){
    Log.v("","We're in pre execture");
}

protected void onProgessUpdate(){}

protected void onPostExecute(List<String> result){
    setResult(result);
}
}
4

1 回答 1

1

改变这个:

String[] str_validsI = new getValids().execute("main").get();

有了这个:

String[] str_validsI = null;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) { // Build.VERSION_CODES.HONEYCOMB = 11
   str_validsI = new getValids().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "main").get();
} else {
   str_validsI = new getValids().execute("main").get();
}

注意:在他们的方法中使用AsyncTasks不是一个好主意。get()因为你消除了它们的本质并使它们同步。

于 2013-11-12T17:35:54.257 回答