My requirement is :
I want to upload the sqlite table records to MS SQL server.I have service.Normal Uploading alos working fine(Normal uploading means after login , it will show list of upload table information.When the user click upload button, it upload & go to next screen). Now i want to do this: sale rep save invoice/payment then service start & upload data in the backend sql server without effecting rest of the code.
I did like this :
public class UploadService extends Service {
private static final String TAG = "UploadService";
public static final String APPURL = "http://192.168.1.213:7986/XontService";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "convertJSONToDataSet";
private static String SOAP_ACTION = "http://tempuri.org/IXontService/convertJSONToDataSet";
ArrayList<String> uploadFiler = new ArrayList<String>();
HashMap<Integer,String> uploadTable = new HashMap<Integer, String>();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Upload Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
uploadUsingService();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Upload Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Upload Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
public void uploadUsingService(){
uploadTable = getUploadTable();
if(uploadTable.size() > 0){
for (Map.Entry<Integer, String> entry : uploadTable.entrySet()) {
System.out.println("----key" + entry.getKey());
String value = entry.getValue();
if(value.equals("WMInvoiceHeader")){
getInvoiceHeader();
}
}
}
}
public void getInvoiceHeader(){
boolean serviceStatus =true;
DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
// dbAdapter.openDataBase();
SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_PRIVATE);
String strBusinessUnit = myPrefs.getString("BusinessUnit", "");
String strExecutive = myPrefs.getString("Executive", "");
try{
StringBuffer invoiceHeader = new StringBuffer();
JSONArray jaHeader = new JSONArray();
String query = "SELECT BusinessUnit,ExecutiveCode,InvoiceNo,SalesCategory,RetailerCode," +//4
" RetailerCodeSon,InvoiceDate,GrossValue,InvoiceValue,TotalLineDiscount," + //9
" TotalHeaderDiscount,VATValue,UpdatedOn,UpdatedBy,UploadFlag,VisitNumber ," + //15
" VisitSequence,RouteCode,SalesType,ActiveStatus,UploadedOn,UploadedBy,UploadedMethod," +
" UploadedType,RetailerType,HeaderDiscountFlag,SpecialDiscountFlag,TXNReference,TourReference" +
" FROM WMInvoiceHeader " +
" WHERE (CancelFlag IS NULL OR CancelFlag ='0')";
ArrayList<?> stringList = dbAdapter.selectRecordsFromDBList(query, null);
if(stringList.size() > 0){
invoiceHeader.append("{\"WMInvoiceHeader\":");
for (int i = 0; i < stringList.size(); i++) {
ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
ArrayList<?> list = arrayList;
JSONObject invHeader = new JSONObject();
invHeader.put("BusinessUnit",(String)list.get(0));
invHeader.put("ExecutiveCode",(String)list.get(1));
invHeader.put("InvoiceDate",(String)list.get(6));
invHeader.put("VisitSequence",(String)list.get(16));
invHeader.put("RouteCode",(String)list.get(17));
invHeader.put("SalesType",(String)list.get(18));
jaHeader.put(invHeader);
}
}
invoiceHeader.append(jaHeader.toString());
invoiceHeader.append("}");
serviceStatus = soapPrimitiveData("WMInvoiceHeader", strBusinessUnit, strExecutive, invoiceHeader.toString());
}catch (IOException e) {
serviceStatus =false;
e.printStackTrace();
} catch (XmlPullParserException e) {
serviceStatus =false;
e.printStackTrace();
}catch (Exception e) {
serviceStatus =false;
e.printStackTrace();
}finally{
if(!serviceStatus){
uploadFiler.add("WMInvoiceHeader");
}
}
}
public boolean soapPrimitiveData(String tablename,String strBusinessUnit, String strExecutive,String jsonString) throws IOException,XmlPullParserException {
SoapPrimitive responsesData = null;
boolean status =false;
SoapObject requestData = new SoapObject(NAMESPACE, METHOD_NAME); // set
requestData.addProperty("strBusinessUnit", strBusinessUnit);
requestData.addProperty("strExecutiveCode", strExecutive);
requestData.addProperty("strTableName", tablename);
requestData.addProperty("jsonContent", jsonString);
SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
envelopes.dotNet = true;
envelopes.setOutputSoapObject(requestData);
AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
httpTransport.debug = true;
try {
httpTransport.call(SOAP_ACTION, envelopes);
responsesData = (SoapPrimitive) envelopes.getResponse();
if((responsesData.toString()).equals("true")){
DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(UploadService.this);
status = dbAdapter.updateUploadedTable(tablename, strExecutive, strBusinessUnit);
}
} catch (SocketException ex) {
status = false;
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
} catch (Exception e) {
status = false;
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return status;
}
public HashMap<Integer,String> getUploadTable(){
DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
dbAdapter.openDataBase();
SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_PRIVATE);
String strBusinessUnit = myPrefs.getString("BusinessUnit", "");
String strExecutive = myPrefs.getString("Executive", "");
HashMap<Integer,String> uploadTableMap = new HashMap<Integer,String>();
try {
String query = "SELECT TableName FROM WMPalmUploadControl WHERE Upload = '1' AND BusinessUnit =? AND ExecutiveCode = ?;";
String[] d = new String[]{strBusinessUnit,strExecutive};
ArrayList<?> stringList = dbAdapter.selectRecordsFromDBList(query, d);
if(stringList.size() > 0){
for (int i = 0; i < stringList.size(); i++) {
ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
ArrayList<?> list = arrayList;
uploadTableMap.put(i, (String)list.get(0));
}
}
dbAdapter.close();
} catch (Exception e) {
e.printStackTrace();
}
return uploadTableMap;
}
}
Here i want to call uploadUsingService() method in the onStart().
This is my calling code:
if(is3g ||isWifi){
startService(new Intent(this, UploadService.class));
}else if (networkType == TelephonyManager.NETWORK_TYPE_GPRS) {
if(signalStrengthInt > 10){ ....}
}
When I ran this code :I got this onCreate()' methodToastmessageUpload Service Created. 'onStart()` Toast message not display. How can i call this. I did this part from this Link
Please anybody help me out from this issue?
service.onStart has been deprecated since Android 2.0. Perhaps it’s no longer called in the version you’re using. Read this: http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
Use service.onStartCommand instead.