I am creating an app that has few profiles that user can create. When they create a profile, it is saved in to sqlite db. Whenever user goes in to the app, it tries to sync itself with mysql db. I am able to access the db but get this error for the entries i am syncing in to the mysql --the Undefined property: stdClass--. I really don't know much php and sql but able to create this using some tutorial. Would really appreciate any detail explaination
public void syncSQLiteMySQLDB(){
//Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
ArrayList<HashMap<String, String>> userList = dbhelper.getAllUsers();
if(userList.size()!=0){
if(dbhelper.dbSyncCount() != 0){
prgDialog.show();
params.put("usersJSON", dbhelper.composeJSONfromSQLite());
Log.d("composed JSONfrom SQLite", "done");
client.post("hidden for posting purpose",params ,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
prgDialog.hide();
try {
JSONArray arr = new JSONArray(response);
System.out.println(arr.length());
for(int i=0; i<arr.length();i++){
JSONObject obj = (JSONObject)arr.get(i);
System.out.println(obj.get("id"));
System.out.println(obj.get("status"));
dbhelper.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString());
}
Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
// TODO Auto-generated method stub
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();
}
}
});
}else{
Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
}
}
DBHELPER
/**
* Compose JSON out of SQLite records
* @return
*/
public String composeJSONfromSQLite(){
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM usersprofile where syncstatus = '"+"no"+"'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", cursor.getString(0));
map.put("androidid", cursor.getString(1));
map.put("logo", cursor.getString(2));
map.put("name", cursor.getString(3));
map.put("phone", cursor.getString(4));
map.put("employeeid", cursor.getString(5));
map.put("jobtitle", cursor.getString(6));
map.put("email", cursor.getString(7));
map.put("companyname", cursor.getString(8));
map.put("address", cursor.getString(9));
map.put("city", cursor.getString(10));
map.put("province", cursor.getString(11));
map.put("postal", cursor.getString(12));
wordList.add(map);
} while (cursor.moveToNext());
}
database.close();
Gson gson = new GsonBuilder().create();
//Use GSON to serialize Array List to JSON
return gson.toJson(wordList);
}
insertuser.php
<?php
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["usersJSON"];
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into DB
$res = $db->storeUser($data[$i]->PID,$data[$i]->AID, $data[$i]->Logo,$data[$i]->Name, $data[$i]->Phone, $data[$i]->Employeeid, $data[$i]->Jobtitle, $data[$i]->Email, $data[$i]->Companyname, $data[$i]->Address, $data[$i]->City, $data[$i]->Province, $data[$i]->Postal);
//Based on inserttion, create JSON response
if($res){
$b[“id”] = $data[$i]->PID;
$b[“syncstatus”] = 'yes';
array_push($a,$b);
}else{$b[“id”] = $data[$i]->PID;
$b[“syncstatus”] = 'no';
array_push($a,$b);}
}
//Post JSON response back to Android Application
echo json_encode($a);
db_functions.php
<?php
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
include_once './db_connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($PID,$AID,$Logo, $Name, $Phone, $Employeeid, $Jobtitle, $Email, $Companyname, $Address, $City, $Province, $Postal) {
// Insert user into database
$result = _query("INSERT INTO usersprofile VALUES(‘$PID’,’$AID', '$Logo', '$Name', '$Phone', '$Employeeid', '$Jobtitle', '$Email', '$Companyname', '$Address', '$City', '$Province', '$Postal’)");
if ($result) {
return true;
} else {
if( _errno() == 1062) {
// Duplicate key - Primary Key Violation
return true;
} else {
// For other errors
return false;
}
}
}
/**
* Getting all users
*/
public function getAllUsers() {
$result = _query("select * FROM usersprofile");
return $result;
}
} ?>
ERROR [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$PID in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$AID in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Logo in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Name in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Phone in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Employeeid in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Jobtitle in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Email in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Companyname in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Address in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$City in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Province in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Notice: Undefined property: stdClass::$Postal in /home2/pctechni/public_html/mirphp/insertuser.php on line 20 [06-Mar-2015 07:56:26 America/Denver] PHP Fatal error: Call to undefined function _query() in /home2/pctechni/public_html/mirphp/db_functions.php on line 27
Aucun commentaire:
Enregistrer un commentaire