gpt4 book ai didi

java - 如何将 HTTPClient 移至异步任务

转载 作者:行者123 更新时间:2023-12-01 04:40:46 24 4
gpt4 key购买 nike

我在使用要转移到 AsyncTask 的方法时遇到问题。我已经完成了 doInBackground,但我需要将方法的其余部分移动到 onPostExecute...我只是不知道如何做。

这是我原来的方法。

private void ShowFuelStopAndRoute(String location) throws IOException {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;

addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.fillingstation);
currentFuelLat= addresses.get(0).getLatitude();
currentFuellong= addresses.get(0).getLongitude();
LatLng toPostion = new LatLng(currentFuelLat, currentFuellong);
GMapV2Direction md = new GMapV2Direction();
AsyncDoc asyncDoc = new AsyncDoc(MapViewActivity.this, currentLocation, toPostion, GMapV2Direction.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(doc);

PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.BLUE);

for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}

map.addPolyline(rectLine);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(toPostion).icon(subwayBitmapDescriptor).title("Fueling Station, " + location));


}
}

我将以下行移动到不同的类和异步类中

Document doc = md.getDocument(currentLocation, toPostion, GMapV2Direction.MODE_DRIVING);

这已被替换为:

return DALController.sharedInstance().getDocument(startLoc, endLoc, mode);

我有这么多 AsyncTask Complete ,如下所示:

    private class AsyncDoc extends AsyncTask<Void, Void, Document > {

public String mode;
public LatLng startLoc;
public LatLng endLoc;


public AsyncDoc (Activity activity, LatLng startLoc, LatLng endLoc, String mode){

this.startLoc = startLoc;
this.endLoc = endLoc;
this.mode = mode;
dialog = new ProgressDialog(activity);
}

private Activity activity;
private ProgressDialog dialog;

protected void onPreExecute() {
this.dialog.setMessage("Progress start");
if (! (activity).isFinishing()) {
this.dialog.show();
}
}


@Override
protected Document doInBackground(Void... params) {
return DALController.sharedInstance().getDocument(startLoc, endLoc, mode);
}

@Override
protected void onPostExecute(Document result) {

if (dialog.isShowing()) {
dialog.dismiss();
}




}

}

但我需要确保在 doInBackground 完成之前其余代码不会运行:

        ArrayList<LatLng> directionPoint = md.getDirection(doc);

PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.BLUE);

for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}

map.addPolyline(rectLine);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(toPostion).icon(subwayBitmapDescriptor).title("Fueling Station, " + location));

如何确保 doInBackground 在 showfuelstopandroute 方法的其余部分完成之前完成?

最佳答案

How can I ensure the doInBackground completes before the rest of my showfuelstopandroute method completes?

您应该需要将所有代码移至 onPostExecute 内,以便在 doInBackground 执行完成后执行。如:

@Override
protected void onPostExecute(Document result) {

if (dialog.isShowing()) {
dialog.dismiss();
}
// do your work here after doInBackground execution
ArrayList<LatLng> directionPoint = md.getDirection(result);
....

}

关于java - 如何将 HTTPClient 移至异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16598686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com