如何等到onPostExecute完成

所以我是编程的新手,我正在做的是制作一个Android应用程序,它将给客户提供最近和最快的电影/加油站/市场方式 . 问题是,首先我需要通过谷歌api找到最近的地方获取拉特和lng然后用它们来指导api . 所以我做的是这样的:

GetNearbyPlaces.java

public class GetNearbyPlaces extends AsyncTask<Object, String, String>
{
private String googleplaceData, url;
private GoogleMap mMap;

@Override
protected String doInBackground(Object... objects)
{
    mMap = (GoogleMap) objects[0];
    url = (String) objects[1];

    DownloadUrl downloadUrl = new DownloadUrl();
    try
    {
        googleplaceData = downloadUrl.ReadTheURL(url);
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }

    return googleplaceData;
}


@Override
protected void onPostExecute(String s)
{

    List<HashMap<String, String>> nearbyPlacesList = null;
    DataParser dataParser = new DataParser();
    nearbyPlacesList = dataParser.parse(s);
    DisplayNearbyPlaces(nearbyPlacesList);
    super.onPostExecute(s);
    Log.d("Message","telos execute ");

}

private void DisplayNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList)
{
    for (int i=0; i<nearbyPlacesList.size(); 
       {
        Log.d("Message","mesa stin for ");
        MarkerOptions markerOptions = new MarkerOptions();
        HashMap<String, String> googleNearbyPlace = nearbyPlacesList.get(i);
        String nameOfPlace = googleNearbyPlace.get("place_name");
        String vicinity = googleNearbyPlace.get("vicinity");

        double lat = Double.parseDouble(googleNearbyPlace.get("lat"));
        double lng = Double.parseDouble(googleNearbyPlace.get("lng"));

        LatLng latLng = new LatLng(lat, lng);

        markerOptions.position(latLng);
        markerOptions.title(nameOfPlace + " : " + vicinity);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
        mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
    }

}

}

GoogleUserMaps.java

case R.id.gasstation_nearby:
            mMap.clear();
            mMap.addMarker(userLocMarkerOptions);
            mMap.setTrafficEnabled(true);

            url = getUrl(latitude,longitude,gasStation);
            transferData[0] = mMap;
            transferData[1] = url;

            Log.d("Message","Prin kanei execute ");
            getNearbyPlaces.execute(transferData);
            Log.d("Message","afou kanei execute ");


            Toast.makeText(this, "Searching for Nearby Gas Stations.", Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "Showing for Nearby Gas Stations.", Toast.LENGTH_SHORT).show();
            break;

我想在DisplayNearbyPlaces里面添加一个公共变量来获取lats和lngs,但是当我在getNearbyPlaces.execute(transferData)之后使用它时.get(); GoogleUserMaps.java给出了0结果 . 我无法理解AsyncTask . 有什么方法可以等到getNearbyPlaces.execute(transferData).get();完成执行以获得lats和lngs?

先感谢您 !

我的logcat I want to get the result after the execute finishes as I show you in the image bellow

回答(3)

2 years ago

异步任务在独立的线程中运行,而不是UI线程 . 如果您尝试使用Callback接口并且当调用onPost of Async任务时执行该回调中的操作会更好

2 years ago

@Strack我发布了接口的代码示例

`

public interface PlaceListener{
     void searchStarted();
     void searchEnded();

}



public class GetNearbyPlaces extends AsyncTask<Object, String, String>
{
private String googleplaceData, url;
private GoogleMap mMap;
private PlaceListener placeListener;

public GetNearbyPlaces(PlaceListener placeListener){
    this.placeListner = placeListener;
}

//onPreExecute(){ placeListner.searchStarted();}

@Override
protected String doInBackground(Object... objects)
{
    mMap = (GoogleMap) objects[0];
    url = (String) objects[1];

    DownloadUrl downloadUrl = new DownloadUrl();
    try
    {
        googleplaceData = downloadUrl.ReadTheURL(url);
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }

    return googleplaceData;
}


@Override
protected void onPostExecute(String s)
{
    placeListner.serachEnded();
    List<HashMap<String, String>> nearbyPlacesList = null;
    DataParser dataParser = new DataParser();
    nearbyPlacesList = dataParser.parse(s);
    DisplayNearbyPlaces(nearbyPlacesList);
    super.onPostExecute(s);
    Log.d("Message","telos execute ");

}


}

`

在Caller Activity中实现PlaceListener并在searchEnded中执行所需的任务 . 您可以根据要传递的所需数据制作接口方法的参数

2 years ago

好 . 创建一个新的回拨:

public interface MyCallBack {
    void myTaskDone(List<HashMap<String, String>> nearbyPlacesList);
}

在您的活动中创建一个:

MyCallBack myCallBack = new MyCallBack() {
    @Override
    public void myTaskDone(List<HashMap<String, String>> nearbyPlacesList) {
        DisplayNearbyPlaces(nearbyPlacesList);
    }
};

发送对Aynctask的引用:

getNearbyPlaces.execute(transferData, map, new MyCallBack() {
        @Override
        public void myTaskDone(List<HashMap<String, String>> nearbyPlacesList) {
            DisplayNearbyPlaces(nearbyPlacesList);
        }
    });

完整代码:

public interface MyCallBack {
    void myTaskDone(List<HashMap<String, String>> nearbyPlacesList);
}

public class GetNearbyPlaces extends AsyncTask<Object, String, String> {
    private String googleplaceData;
    private String url;
    private GoogleMap mMap;
    private MyCallBack myCallBack;

    public GetNearbyPlaces(String url, GoogleMap mMap, MyCallBack myCallBack) {
        this.url = url;
        this.mMap = mMap;
        this.myCallBack = myCallBack;
    }

    @Override
    protected String doInBackground(Object... objects) {
        DownloadUrl downloadUrl = new DownloadUrl();
        try {
            googleplaceData = downloadUrl.ReadTheURL(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return googleplaceData;
    }


    @Override
    protected void onPostExecute(String s) {
        List<HashMap<String, String>> nearbyPlacesList = null;
        DataParser dataParser = new DataParser();
        nearbyPlacesList = dataParser.parse(s);
        myCallBack.myTaskDone(nearbyPlacesList);
    }

}

private void DisplayNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList) {
    /// your code
}

MyCallBack myCallBack = new MyCallBack() {
    @Override
    public void myTaskDone(List<HashMap<String, String>> nearbyPlacesList) {
        DisplayNearbyPlaces(nearbyPlacesList); 
    }
};

怎么称呼它:

//.....
    transferData[0] = mMap;
    transferData[1] = url;

    Log.d("Message","Prin kanei execute ");
            GetNearbyPlaces getNearbyPlaces = new GetNearbyPlaces(transferData, map, myCallBack);
    getNearbyPlaces.execute();

    //or:

    GetNearbyPlaces getNearbyPlaces = new GetNearbyPlaces(transferData, map, new MyCallBack() {
        @Override
        public void myTaskDone(List<HashMap<String, String>> nearbyPlacesList) {
            DisplayNearbyPlaces(nearbyPlacesList);
        }
    });
    getNearbyPlaces.execute();