I'm calling this method where ever the service needs to be started
public void syncData(){
Intent mServiceIntent = new Intent(this, DataSyncToServer.class);
Log.d("SyncDAta", "Starting Intent Service");
this.startService(mServiceIntent);
}
This is my service class
public class DataSyncToServer extends IntentService {
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://***/**";
final String SOAP_ACTION_SaveTrip = "http://ift.tt/1RS43La";
long id;
protected void onHandleIntent(Intent workIntent) {
Log.d("DataSyncService", "Inside on handleIntent");
if(new Tracker().isNetworkAvailable()){
Log.d("DataSync Network Check", "network is available");
updateTripPrice();
Log.d("DataSync TripPrice After", "starting cabtrans update");
new NriTrackerDb(getApplicationContext()).deleteTrips();
}
}
public DataSyncToServer(String name) {
super(name);
}
public void updateTripPrice(){
Log.d("DataSync updTripPrice", "just entered it");
ArrayList<TripPrice> al = new NriTrackerDb(getApplicationContext()).getTrips();
if(al!=null){
Log.d("DataSync updTripPrice", "retrieved data from database.");
for(int i = 0;i < al.size(); i++){
TripPrice tr = al.get(i);
id = tr.trip_id;
SoapObject saveTrip = new SoapObject(NAMESPACE,"SaveTrip");
saveTrip.addProperty("cab_no",tr.cab_no);
saveTrip.addProperty("mobile_no",tr.mobile_no);
saveTrip.addProperty("ticket_nbr",String.valueOf(tr.trip_id));
saveTrip.addProperty("distance",String.valueOf(tr.distance));
saveTrip.addProperty("amount",String.valueOf(tr.amount));
saveTrip.addProperty("trip_start",String.valueOf(tr.trip_start));
saveTrip.addProperty("trip_end",String.valueOf(tr.trip_end));
Log.d("DataSync updTripPrice", "calling async task");
new SaveTrip().execute(saveTrip);
Log.d("DataSync updTripPrice", "after async task");
}
}
}
public class SaveTrip extends AsyncTask<SoapObject,Void,Void>{
@Override
protected Void doInBackground(SoapObject... request) {
Log.d("DataSync AsyncTask", "AsyncTask do in background entered");
try{
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request[0]);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
Log.d("DataSync AsyncTask", "before calling the web api");
androidHttpTransport.call(SOAP_ACTION_SaveTrip, envelope);
Log.d("DataSync AsyncTask", "called api successfully");
androidHttpTransport.debug = true;
Log.d("DataSync AsyncTask", "before taking response");
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.d("DataSync AsyncTask", "response is taken successfully");
System.out.println(response);
Log.d("DataSync AsyncTask", "at the end of doInBackground");
new NriTrackerDb(getApplicationContext()).updateTripPrice(id);
} catch (XmlPullParserException | IOException e) {
Log.i("Exception in doInBackground of SaveTrip","Trying to handle exception here");
}
}catch(Exception e){
Log.d("in the service method","failed sending trips to database");
}
return null;
}
}
}
And this is the method of my db
public ArrayList<TripPrice> getTrips(){
Log.d("Database GetTrips", "just entered");
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM trip_price WHERE push_data = 'NO'";
ArrayList<TripPrice> tp = new ArrayList<TripPrice>();
Log.d("Database GetTrips", "before query");
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("Database GetTrips", "after query");
if(cursor != null && cursor.moveToFirst()){
Log.d("Database GetTrips", "query successful");
do{
TripPrice tr = new TripPrice();
tr.cab_no = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_CAB));
tr.mobile_no = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_MOBILE_NO));
tr.trip_id = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_TRIP_ID));
tr.distance = cursor.getDouble(cursor.getColumnIndex(COLUMN_NAME_DISTANCE));
tr.amount = cursor.getDouble(cursor.getColumnIndex(COLUMN_NAME_AMOUNT));
tr.trip_start = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_TRIP_START));
tr.trip_end = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_TRIP_END));
tp.add(tr);
Log.d("Database GetTrips", "in the loop");
}while(cursor.moveToNext());
db.close();
Log.d("Database GetTrips", "sending data");
return tp;
}
else
db.close();
return null;
}
I'm not able to check weather this service is working or not. SO I'm checking for new data in back end, but there is no data that is sent by this service. Need suggestions badly.
Aucun commentaire:
Enregistrer un commentaire