DefaultHttpClient execute()方法有时不返回

时间:2011-07-03 12:13:04

标签: android

在我的应用程序中,我使用共享DefaultHttpClient来执行所有http请求。在我的一个用户界面中,有ListView用于显示服务器请求的数据,还有一个'查询更多'按钮,用于通过启动AsycnTask来执行请求,从服务器请求更多数据。

ListView的项目是可点击的。单击一个项目时,我的应用程序将启动一个新活动,用于显示所单击项目的详细信息数据。详细数据请求在另一个AsyncTask中执行,并且存在问题:当我单击“查询更多”按钮然后几乎在同一时间(可能稍后)我单击ListView的一个项目,后一个请求(详细数据)通常会被回复,但前一个请求(更多数据)似乎停留在doInBackground AsyncTask并且永远不会返回,任何想法?

以下是“查询更多”任务的代码:

private class SearchTask extends AsyncTask<Void, Void, Group<Venue>> {

    private Exception mReason = null;
    private boolean mMore = false;
    private int mFrom;

    public SearchTask(boolean moreSearch,int startFrom){
        mMore = moreSearch;
        mFrom = startFrom;
    }

    @Override
    public void onPreExecute() {
        if (DEBUG) Log.d(TAG, "SearchTask: onPreExecute()");
        if(!mMore) {
            setProgressBarIndeterminateVisibility(true);
            setLoadingView();               
        } 
    }

    @Override
    public Group<Venue> doInBackground(Void... params) {
        try {
            return search(mFrom,Anywhered.QUERY_LIMIT);
        } catch (Exception e) {
            mReason = e;
        }
        return null;
    }

    @Override
    public void onPostExecute(Group<Venue> venues) {  
               ..... //omitted
           }

    public Group<Venue> search(int from,int limit) throws AnywhereException, LocationException,
            IOException {

        Anywhere anywhere = ((Anywhered) getApplication()).getAnywhere();

        Location location = ((Anywhered) getApplication()).getLastKnownLocationOrThrow();

        NearbyVenues nvs  = anywhere.venues(LocationUtils
                .createAnywhereLocation(location), mSearchHolder.query,from, limit);
        Group<Venue> venues = nvs.getNearybys();

        return venues;
    }
}

venues method will eventually call DefaultHttpClient execute() method to perform request data from server.

0 个答案:

没有答案