AsyncTask仅在我调试时有效

时间:2012-08-06 22:52:14

标签: android

我有一个Android活动,可以启动AsyncTasks查询Web服务的数据。当我调试(添加一个断点,并在每次迭代时单击继续)时,一切正常。当我在没有调试的情况下运行应用程序时,它总会引发多个间歇性错误,例如:

java.io.FileNotFoundExceptions:http://mywebservice.aspx?op=getData&aid=28559,28555

我已经检查了错误中的所有网址,它们都返回了数据。我还强调测试了webservice,它返回了数百个请求而没有失败。

该应用程序会在每次查询时启动20个AsyncTasks,并且通常几乎所有这些都会在我没有调试的情况下运行应用程序时失败,如果我添加一个断点,所有这些都会成功。

以下是代码的摘录......

public class LoadDataTask extends AsyncTask <IPResultItem,Integer,Long>{
    private MobAdXActivity _ctx=null;
    private MobileArtDataInfo _info=null;
    private String _content="";
    private int _height;
    private String _book;
    private String _tag;
    private int _index;

    public LoadDataTask(MobAdXActivity ctx,int height,String book, String viewtag,int index){
        _ctx=ctx;
        _height=height;
        _book=book;
        _tag=viewtag;
        _index=index;
    }

    @Override
    protected Long doInBackground(IPResultItem... itms) {
        long count = itms.length;
         if (count>0)
             getData(itms[0]);

         return count;
     }

     protected void onProgressUpdate(Integer... progress) {
     }

     protected void onPostExecute(Long result) {
         _ctx.setViewContent(_tag, _content, _info, _index);
     }

    //I'm querying a webservice for data here
    public void getData(IPResultItem itm){
        YPResult ypres = bus.queryWebService(itm);
        if (ypres!=null){
            _content = ypres.Content;
           _info=ypres.Info;
        }
    }
}

//摘自我的活动...... 公共类MobAdActivity扩展了MobAdXActivity {

// This method kicks off the AsyncTasks
public void loadData(){
    if (_itms!=null){
        for (int i=0;i<this._count ;i++){
            // I store a ref to the task so that I can cancel it if needed
            _ldTasks.add((LoadDataTask)new LoadDataTask((MobAdXActivity)_ctx,_dm.heightPixels - BTN_BAR_HEIGHT,_bkcodes.get(sBk),"web" + i,i).execute(_itms[i]));
        }
    }
}

// And here I set the results
@Override
public void setViewContent(String pViewTag, String pContent,MobileArtDataInfo pInfo,int pIndex) {
    HorizontalScrollView hsv=(HorizontalScrollView)findViewById(R.id.hsvWeb);
    WebView web=(WebView)hsv.findViewWithTag(pViewTag);

    web.loadDataWithBaseURL(bus.MOBAD_IMG_ROOT , pContent, "text/html","UTF-8" ,null);
    web.invalidate();
}

}

// MobAdXActivity只包含AsyncTask回调的方法签名

public abstract class MobAdXActivity extends Activity {
    public abstract void setViewContent(String pViewTag, String pContent,MobileArtDataInfo pInfo, int pIndex);
}

更新

我无法确定它是否在Android 3.0中正常运行,因为有很多依赖项,而我无法移植它。

重新。请求参数,我已经通过将它们复制/粘贴到我的浏览器中,在Logcat中的错误消息中直观地验证了URL,并从webservice返回了正确的feed。

在启动asynctasks的循环中添加延迟似乎可以解决问题,但是,在ui线程上引入10秒延迟(20个异步通信中的每一个的1/2秒)是不可行的。

0 个答案:

没有答案