【问题标题】:How to move HTTPClient to async Task如何将 HTTPClient 移动到异步任务
【发布时间】:2013-05-16 22:41:08
【问题描述】:

我在迁移到 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 方法的其余部分完成之前完成?

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

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

    您应该需要将您想要在 doInBackground 执行完成后执行的 onPostExecute 中的所有代码移动。如:

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

    【讨论】:

      【解决方案2】:

      在 AsyncTask 中,onPostExecute() 方法仅在 doInBackground() 完成后运行。您面临的问题是什么?

      【讨论】:

        【解决方案3】:

        正如其他人所提到的,您可以在其 postExecute 方法中移动应该在任务之后执行的整个代码。 另一种方法是让您的活动有一个帽子方法,该任务可以在其 onPostExecute() 中调用它

        【讨论】:

        • 如果我将整个 showFuelStopRoute 移到 AsyncTask 中会怎样?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多