异步doInBackground没有被调用

时间:2012-08-07 07:38:31

标签: android android-asynctask

在下面的类中,使用异步内部类函数doInBackground没有获得命中。在调用rpi.excute然后尝试通过HTTP下载的getQuestionnaireBO获取数据时,我在AndroidManifest.xml和其他配置中拥有Internet权限

public class RestApiClient {
    private final static String SERVICE_URI = "http://182.50.154.23/Dattab.Device.Rest/ServiceClass/RestDeviceService.svc";
    private QuestionnaireBO resultQuestionnaireBO ;
    public void Questionnairedownload(int QuestionnaireId ) {
        String url=SERVICE_URI+"/DownloadQuestionnaireData/"+QuestionnaireId;
        // InputStream source = retrieveStream(url);
        // Gson gson = new Gson();
        // Reader reader = new InputStreamReader(source);
        // QuestionnaireBO response = gson.fromJson(reader, QuestionnaireBO.class);
        ChildThreadQuestionnairedownload ctqd = new ChildThreadQuestionnairedownload();
        ctqd.execute(url);

        // return null ;        
    }

    public QuestionnaireBO getQuestionnaireBO() {
        return resultQuestionnaireBO;
    }


    private class ChildThreadQuestionnairedownload extends AsyncTask<String,Integer,QuestionnaireBO> {

        @Override
        protected QuestionnaireBO doInBackground(String... params) {
             String url=params[0];
             Gson gson = new Gson();
             DefaultHttpClient client = new DefaultHttpClient();                     
             HttpGet getRequest = new HttpGet(url);
             try {
                 HttpResponse getResponse = client.execute(getRequest);
                 final int statusCode = getResponse.getStatusLine().getStatusCode();

                 if(statusCode != HttpStatus.SC_OK) {
                      Log.w(getClass().getSimpleName(),
                            "Error " + statusCode + " for URL " + url);
                      return null;
                 }

                 HttpEntity getResponseEntity = getResponse.getEntity();
                 Reader reader = new InputStreamReader(getResponseEntity.getContent());
                 QuestionnaireBO response = gson.fromJson(reader, QuestionnaireBO.class);

                  return response ;
             }
             catch(IOException e){
                  e.printStackTrace();
                  return null;
             }

         }

         protected void onPostExecute(QuestionnaireBO response) {
             resultQuestionnaireBO=response;
         }

     }
}

1 个答案:

答案 0 :(得分:0)

我没有看到为什么doInBackground()不会被调用的任何原因。

这可能是一个转储,但是......你确定你有互联网连接吗? 有时候,如果你使用的是Wi-Fi连接可能会丢失,如果你使用3G,可能就是你花了所有的钱。所以,无法访问互联网。