如何等待onPostExecute完成

时间:2018-12-10 11:34:15

标签: android asynchronous google-maps-api-3 android-asynctask

因此,我是编程新手,我想做的是制作一个Android应用程序,该程序将向客户提供前往电影院/加油站/市场的最近,最快的方式。问题是,首先我需要通过google api找到最近的地方,获取经纬度,然后使用它们来确定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内添加一些公共变量以获取经纬度,但是当我在getNearbyPlaces.execute(transferData).get()之后使用它时, GoogleUserMaps.java给出了0个结果。我听不懂AsyncTask。有什么办法可以等到 getNearbyPlaces.execute(transferData).get();完成执行以获取经度和纬度?

提前谢谢!

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

3 个答案:

答案 0 :(得分:0)

异步任务在与UI线程不同的线程中运行。最好使用Callback接口,并在调用onPost of Async任务时执行该回调中的操作

答案 1 :(得分:0)

@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 ");

}


}

`

在“调用者活动”中实现PlaceListener并在searchEnded中执行所需的任务。您可以根据想要传递的数据来设置接口方法的参数

答案 2 :(得分:0)

好。创建新的回叫:

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();