如何在每一秒钟内连续呼叫Web服务?

时间:2014-04-21 10:06:06

标签: android web-services

allVehiclesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                String item=allVehiclesSpinner.getItemAtPosition(position).toString();

                if(!item.equalsIgnoreCase("Select")){
                    position=0;

                    vdetails.vehicleNo = item;
                    dbhHelper.opendatabase();
                    vdetails=dbhHelper.getVehicleId(item);
                    dbhHelper.close();




                    getLiveTrackingsAsyncTask livetask=new getLiveTrackingsAsyncTask();                 
                    livetask.execute();                     

                    }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });

public class getLiveTrackingsAsyncTask extends AsyncTask<String, Void, String> {

    private ProgressDialog Dialog;

    @Override
    protected void onPreExecute() {
        Dialog = new ProgressDialog(VTS_LiveTracking.this);
        Dialog.setMessage(VTS_LiveTracking.this.getResources().getString(
                R.string.loading));
        Dialog.setCanceledOnTouchOutside(false);
        Dialog.show();
        Dialog.setContentView(R.layout.progress);
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... params) {
        String result="";
        Log.d(WebServiceHelper.TAG, "AsyncTask>>>>>>>>>>>");
        if(isInternetPresent){
            try {
                Log.d(WebServiceHelper.TAG, "AsyncTask CHeck Internet>>>>>>>>>>>");
                Log.d(WebServiceHelper.TAG, "VehicleId CHeck Internet>>>>>>>>>>>"+vdetails.vehicleId);

                vd=WebServiceHelper.getLiveTrackingDetails(ClientCode, SecretCode, vdetails.vehicleId);
                //vd=WebServiceHelper.getLiveTrackingDetails(ClientCode, SecretCode, "2");  

                Log.d(WebServiceHelper.TAG, "VD>>>>>>>>"+vd.length);
                for(int i=0;i<vd.length;i++){

                    Log.d(WebServiceHelper.TAG, "LiveTrackingElements1>>>>>>>>>>>>>>"+vd[i].deviceMobNo);
                    Log.d(WebServiceHelper.TAG, "LiveTrackingElements2>>>>>>>>>>>>>>"+vd[i].driverName);
                    vdetails.latitude=vd[i].latitude;
                    vdetails.longitude=vd[i].longitude;

                    Log.d(WebServiceHelper.TAG, "vdetails.latitudeLiveTrackingElements1>>>>>>>>>>>>>>"+vdetails.latitude);
                    Log.d(WebServiceHelper.TAG, "vdetails.longitudeLiveTrackingElements2>>>>>>>>>>>>>>"+vdetails.longitude);



                }


            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            result="Success";
        }else{              
            result="Failure";
}
        return result;
    }
    @Override
    protected void onPostExecute(String result) {
        if(result.contentEquals("Success")){
            Log.d(WebServiceHelper.TAG, "AsyncTaskonPostExecute>>>>>>>>>>>");
            Log.d(WebServiceHelper.TAG, "statuslogin>>>>>>>>>>>"+vdetails.status_login);
            if(vdetails.status_login.equals("success")){

                String lat=vdetails.latitude;
                String lon=vdetails.longitude;
                latitude=Double.parseDouble(vdetails.latitude);
                longitude=Double.parseDouble(vdetails.longitude);
                Log.d(WebServiceHelper.TAG, "latitude>>>>"+vdetails.latitude);
                Log.d(WebServiceHelper.TAG,"longitude>>>>"+vdetails.longitude);
                latlng=new LatLng(latitude, longitude);
                 Marker m=map.addMarker(new MarkerOptions().position(latlng));
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15.0f));


                    map.setOnMarkerClickListener(new OnMarkerClickListener() {

                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            final Dialog dialog = new Dialog(VTS_LiveTracking.this);
                            dialog.setContentView(R.layout.custom_dialog);
                            dialog.setTitle("Report");

                            TextView driver, speed,latitude,longitude,last_updated_time,gps_status,
                             driver_mobile_no,vehicle_idle_status,ac_status,device_mobile_no,engine_status;

                            driver = (TextView)dialog.findViewById(R.id.drivernametxt);
                        speed=(TextView)dialog.findViewById(R.id.speedvaluetxt);
                            latitude=(TextView)dialog.findViewById(R.id.latitudetxt);
                            longitude=(TextView)dialog.findViewById(R.id.longitudetxt);
                            last_updated_time=(TextView)dialog.findViewById(R.id.last_updated_timetxt);
                            gps_status=(TextView)dialog.findViewById(R.id.gps_statustxt);
                            driver_mobile_no=(TextView)dialog.findViewById(R.id.driver_mobile_notxt);
                            vehicle_idle_status=(TextView)dialog.findViewById(R.id.vehicle_idle_statustxt);
                            ac_status=(TextView)dialog. findViewById(R.id.ac_statustxt);
                            device_mobile_no=(TextView)dialog.findViewById(R.id.device_mobile_notxt);
                            engine_status=(TextView)dialog.findViewById(R.id.engine_statustxt);

                            driver.setText(vdetails.driverName);
                        speed.setText(vdetails.vehicleSpeed);
                            latitude.setText(vdetails.latitude);
                            longitude.setText(vdetails.longitude);
                            last_updated_time.setText(vdetails.lastUpdatedDateTime);
                            gps_status.setText(vdetails.gpsStatus);
                            driver_mobile_no.setText(vdetails.driverMobNo);
                        vehicle_idle_status.setText(vdetails.vehicleIdleStatus);
                            ac_status.setText(vdetails.acStatus);
                            device_mobile_no.setText(vdetails.deviceMobNo);
                            engine_status.setText(vdetails.engineStatus);
                            dialog.show();

                            Button cancel=(Button) dialog.findViewById(R.id.okbtn);

                            cancel.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();                                   
                                }
                            });

                            return false;

                        }
                    });



            }else{
                Toast.makeText(getApplicationContext(), vdetails.failReason, Toast.LENGTH_LONG).show();
            }
        }else if(result.contentEquals("Failure")){

            Toast.makeText(getApplicationContext(), "Error in Login", Toast.LENGTH_LONG).show();
        }
        super.onPostExecute(result);
        Dialog.dismiss();
        Dialog=null;
    }

}
}

您好,
    对于我的应用程序,非常有必要在特定位置显示车辆的位置。车辆的位置由json webservices给出。车辆正在移动并且位置由webservice更新。但是根据我的代码车辆没有移动。如何在我的应用程序中实现车辆的移动?如何使用计时器实现位置更改?还有其他方法可以实现吗?     请帮我     这是我的代码

3 个答案:

答案 0 :(得分:2)

使用Timer ..

mytimer = new Timer();
final TimerTask mytask = new TimerTask() {
    public void run() {
       //your code here                     
}
});


mytimer.scheduleAtFixedRate(mytask, int delay, int time);

答案 1 :(得分:0)

班级启动服务

allVehiclesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                String item=allVehiclesSpinner.getItemAtPosition(position).toString();

                if(!item.equalsIgnoreCase("Select")){
                    position=0;

                    vdetails.vehicleNo = item;
                    dbhHelper.opendatabase();
                    vdetails=dbhHelper.getVehicleId(item);
                    dbhHelper.close();

               Intent intent = new Intent(A.this,
                Service_A.class);
    startService(intent);

                    }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });

使用服务更新1 SEC后的WEBSERVICE

Service_ A extend service
{
private  Handler handler = new Handler();

@Override
    public void run() {
        // TODO Auto-generated method stub
         handler.postDelayed(Service_ A.this,1000);

    }
//do you code
}

答案 2 :(得分:0)

尝试使用线程调用异步任务,每隔一秒使用代码替换

 getLiveTrackingsAsyncTask livetask=new getLiveTrackingsAsyncTask();                 
 livetask.execute();    

Thread thread = new Thread(new Runnable() {
         @Override
         public void run() {

         try {
         Thread.sleep(1 * 1000);
         new getLiveTrackingsAsyncTask.execute();                 

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

         thread.start();