I cant seem to get the right parameters when I create an new DatabaseHandler
I have tried this every which way and it works fine when the code is in the main activity using
DatabaseHandler db = new DatabaseHandler(this);
I cant figure out what I have to enter for the parameters when its called from another class
The code is below
package com.example.micheal.scannerv1;
import android.content.Context;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by micheal on 19/10/2015.
*/
public class ParseOrderList {
private String result;
private ArrayList<OrderList> orderLists;
// Database Version
private static final int DATABASE_VERSION = 1;
Context mContext;
DatabaseHandler db;
// Database Name
private static final String DATABASE_NAME = "Magento";
ArrayList<String> items=new ArrayList<String>();
public ParseOrderList(Context mContext){
this.mContext=mContext;
this.db = new DatabaseHandler(mContext);
}
public ParseOrderList(String result) {
this.result = result;
orderLists= new ArrayList<>();
}
public ArrayList<OrderList> getOrderLists() {
return orderLists;
}
public boolean process(){
boolean status = true;
OrderList currentRecord = null;
boolean inEntry = false;
String textValue = "";
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(this.result));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
switch(eventType){
case XmlPullParser.START_TAG:
Log.d("Parse OrderList","Starting tag for " + tagName);
if(tagName.equalsIgnoreCase("item")){
inEntry = true;
currentRecord = new OrderList();
}
break;
case XmlPullParser.TEXT:
textValue = xpp.getText();
break;
case XmlPullParser.END_TAG:
Log.d("Parse OrderList","Ending tag for " + tagName);
if(inEntry){
if(tagName.equalsIgnoreCase("item")){
orderLists.add(currentRecord);
inEntry = false;
Log.d ("progress","this far");
} else if(tagName.equalsIgnoreCase("increment_id")){
currentRecord.setIncrement_id(textValue);
} else if(tagName.equalsIgnoreCase("customer_id")){
currentRecord.setCustomer_id(textValue);
} else if(tagName.equalsIgnoreCase("grand_total")){
currentRecord.setGrand_total(textValue);
}
}
break;
default:
//Nothing else to do
}
eventType = xpp.next();
}
Log.d ("progress","this far2");
} catch (Exception e){
status = false;
e.printStackTrace();
}
for (OrderList ord : orderLists){
Log.d ("Customer ", "is " + ord.getCustomer_id());
Log.d ("Grand Total ", "is " + ord.getGrand_total());
Log.d ("Increment ID ", "is " + ord.getIncrement_id());
}
return true;
}
}
DatabaseHandler
package com.example.micheal.scannerv1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by micheal on 25/10/2015.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Magento";
// Contacts table name
private static final String TABLE_CONTACTS = "orders";
// Contacts Table Columns names
private static final String KEY_INCREMENT_ID = "increment_id";
private static final String KEY_CUSTOMER_ID = "customer_id";
private static final String KEY_GRANDTOTAL = "grandTotal";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_INCREMENT_ID + " INTEGER PRIMARY KEY," + KEY_CUSTOMER_ID + " TEXT,"
+ KEY_GRANDTOTAL + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CUSTOMER_ID, contact.getName()); // Contact Name
values.put(KEY_GRANDTOTAL, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_INCREMENT_ID,
KEY_CUSTOMER_ID, KEY_GRANDTOTAL }, KEY_INCREMENT_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return contact;
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CUSTOMER_ID, contact.getName());
values.put(KEY_GRANDTOTAL, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_INCREMENT_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_INCREMENT_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
DownloadData
package com.example.micheal.scannerv1;
import android.os.AsyncTask;
import android.util.Log;
import com.example.michealsadleir.scannerv1.ParseOrderList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalHashtable;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.Hashtable;
/**
* Created by micheal on 26/10/2015.
*/
public class DownloadData extends AsyncTask<Void, Void, String> {
private String mFileContents;
private String downLoadXMLFile;
private static final String NAMESPACE = "urn:Magento";
private static final String URL = "http://www..com.com/api/v2_soap";
public String requestDump = null;
public String resultDump = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
SoapObject request = new SoapObject(NAMESPACE, "login");
request.addProperty("username", "");
request.addProperty("apiKey", "");
env.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call("", env);
env.dotNet = false;
Object result = env.getResponse();
Log.d("sessionId", result.toString());
String sessionId = result.toString();
//Making a hashtable which outputs xml to pass as a filter in the request
Hashtable<String, String> map = new Hashtable<String, String>();
map.put("status", "processing");
// Add another filter by adding something like the line below
// map.put("increment_id", "100001886");
Log.d("hashmapValue", map.toString());
// found out how to add an array to the soap request as magento needs a nested array
// to be sent in ksoap2 but it doesnt support this so the work around below worked
// with a hash table
// https://stackoverflow.com/questions/17342327/in-android-how-to-send-complex-array-in-magento-using-ksoap2-library/25974038#25974038
SoapObject EntityArray = new SoapObject(NAMESPACE, "shoppingCartProductEntityArray");
EntityArray.addProperty("filter", map);
SoapObject requestCart = new SoapObject(NAMESPACE, "salesOrderList");
requestCart.addProperty("sessionId", sessionId);
requestCart.addProperty("filters", EntityArray);
(new MarshalHashtable()).register(env);
env.setOutputSoapObject(requestCart);
androidHttpTransport.call("", env);
requestDump = androidHttpTransport.requestDump;
Log.d("request", requestDump.toString());
(new MarshalHashtable()).register(env);
resultDump = androidHttpTransport.responseDump;
Log.d("result", resultDump.toString());
} catch (Exception e) {
e.printStackTrace();
}
return resultDump;
}
@Override
protected void onPostExecute(String result) {
// take the results from the soap request and pass them to the parser
ParseOrderList parseOrderList = new ParseOrderList(resultDump);
parseOrderList.process();
Log.d("Parsed data", parseOrderList.getOrderLists().toString());
}
}