1

したがって、この AsyncTask getValids をメソッドで呼び出します。パート I はログに記録されますが、パート II は記録されません

     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 IS が呼び出されます

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();
}

注:AsyncTaskメソッドで sを使用することはお勧めできませんget()。あなたはそれらの性質を排除し、それらを同期させるからです.

于 2013-11-12T17:35:54.257 に答える