【问题标题】:Async doInBackground not Getting Called异步 doInBackground 没有被调用
【发布时间】:2012-08-07 07:38:31
【问题描述】:

在下面的类中,使用异步内部类函数 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;
         }

     }
}

【问题讨论】:

  • 你在哪里调用这个方法 Questionnairedownload(123);
  • 你是怎么意识到doInBackground() 方法没有被调用的?
  • 您是否尝试过调试(带断点)?
  • 您在 UI 之外使用 AsyncTask 的任何原因?也许您只需要一个好的老式线程?
  • @PadmaKumar 我从其他类中调用它,例如 RestApiClient client=new RestApiClient(); client.Questionnairedownload(obj.QuestionnaireId); QuestionnaireBO data=client.getQuestionnaireBO();

标签: android android-asynctask


【解决方案1】:

我看不出doInBackground() 不会被调用的任何原因。

这可能是转储文件,但是...您确定您的互联网连接正常吗?
有时,如果您使用的是 wi-fi,则可能会丢失连接,而如果您使用的是 3G,则可能是您花光了所有的钱。所以,不能上网。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多