I having some problem in retrieving the data from MySQL database to the android Sqlite. The problem is when i clicked the sync button and it successful reach to the php file at MySQL phpadmin that will get all the data in table and my php file work well but the data doesn't pass into the Sqlite that I created.
here the method sync MySQL data to sqlite.
public void syncSQLiteMySQLDB() {
// Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
// Http Request Params Object
RequestParams params = new RequestParams();
// Show ProgressBar
prgDialog.show();
// Make Http call to getusers.php
client.post("http://ift.tt/1Zbb7ni", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
// Hide ProgressBar
prgDialog.hide();
// Update SQLite DB with response sent by getusers.php
updateSQLite(response);
}
// When error occured
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// TODO Auto-generated method stub
// Hide ProgressBar
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}
});
}
here the method update the sqlite after sync
public void updateSQLite(String response){
ArrayList<HashMap<String, String>> productsynclist;
productsynclist = new ArrayList<HashMap<String, String>>();
// Create GSON object
Gson gson = new GsonBuilder().create();
try {
// Extract JSON array from the response
JSONArray arr = new JSONArray(response);
System.out.println(arr.length());
// If no of array elements is not zero
if(arr.length() != 0){
// Loop through each array element, get JSON object which has userid and username
for (int i = 0; i < arr.length(); i++) {
// Get JSON object
JSONObject obj = (JSONObject) arr.get(i);
System.out.println(obj.get("productID"));
System.out.println(obj.get("productName"));
System.out.println(obj.get("productNmD"));
System.out.println(obj.get("price"));
System.out.println(obj.get("productDescrp"));
// DB QueryValues Object to insert into SQLite
// Add userID extracted from Object
queryValues = new HashMap<String, String>();
// Add userID extracted from Object
queryValues.put("productID", obj.get("productID").toString());
// Add userName extracted from Object
queryValues.put("productName", obj.get("productName").toString());
queryValues.put("productNmD", obj.get("productNmD").toString());
queryValues.put("price", obj.get("price").toString());
queryValues.put("productDescrp", obj.get("productDescrp").toString());
// Insert User into SQLite DB
controller.insertProduct(queryValues);
// Insert product into SQLite DB
//controller.insertProduct(record);
HashMap<String, String> map = new HashMap<String, String>();
// Add status for each User in Hashmap
map.put("productId", obj.get("productID").toString());
map.put("status", "1");
productsynclist.add(map);
}
// Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
updateMySQLSyncSts(gson.toJson(productsynclist));
// Reload the Main Activity
reloadActivity();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the it will inform remote MySQL DB about completion of Sync activity
// Method to inform remote MySQL DB about completion of Sync activity
public void updateMySQLSyncSts(String json) {
System.out.println(json);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("syncsts", json);
// Make Http call to updatesyncsts.php with JSON parameter which has Sync statuses of Users
client.post("http://ift.tt/1QLryE6", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int statusCode, Throwable error, String content) {
Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_LONG).show();
}
});
}
here my create sqlite db
public class DBController extends SQLiteOpenHelper {
public DBController(Context applicationcontext) {
super(applicationcontext, "product.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query;
query = "CREATE TABLE products ( productID INTEGER, productName TEXT, productNmD TEXT, price TEXT, productDescrp TEXT)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query;
query = "DROP TABLE IF EXISTS users";
db.execSQL(query);
onCreate(db);
}
public void insertProduct(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("productID", queryValues.get("productID"));
values.put("productName", queryValues.get("productName"));
values.put("productNmD", queryValues.get("productNmD"));
values.put("price", queryValues.get("price"));
values.put("productDescrp", queryValues.get("productDescrp"));
database.insert("products", null, values);
database.close();
}
public ArrayList<HashMap<String, String>> getAllProducts() {
ArrayList<HashMap<String, String>> productList;
productList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM products";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("productID", cursor.getString(0));
map.put("productName", cursor.getString(1));
map.put("productNmD", cursor.getString(2));
map.put("price", cursor.getString(3));
map.put("productDescrp", cursor.getString(4));
productList.add(map);
} while (cursor.moveToNext());
}
database.close();
return productList;
}
}
I was referring the method from this article http://ift.tt/ZYxP8Q
thank in advance.
Aucun commentaire:
Enregistrer un commentaire