我想發送我的SQLite數據與在線MySQL服務器,但無濟於事。當然,我跑到谷歌,很幸運地找到this。顯然,它應該工作,但它沒有,但我沒有收到我的服務器上的數據。如何將Android的SQLite數據發送到MySQL服務器?
我知道這個問題已被要求here和here,但我還沒有能夠使用給出的建議修補它。
這是我試過的。這就是我使用GSON轉換我的SQLite數據轉換成JSON:
public String composeJSONfromSQLite() {
ArrayList<HashMap<String, String>> offlineList;
offlineList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM offlineTable ";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("zip", cursor.getString(1));
map.put("phone", cursor.getString(2));
map.put("uid", cursor.getString(3));
offlineList.add(map);
} while (cursor.moveToNext());
}
database.close();
Gson gson = new GsonBuilder().create();
//Use GSON to serialize Array List to JSON
return gson.toJson(offlineList);
}
這是怎麼我把它發送到我的服務器:
public void syncSQLiteMySQLDB() {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("offline",loadCheckoutDB.composeJSONfromSQLite());
Log.d("offline data log", loadCheckoutDB.composeJSONfromSQLite());
client.addHeader("session_id", getapikey());
client.addHeader("Content-Type", "application/json");
client.post("http://example.com/offline/api", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String s = new String(responseBody);
Log.d("response to sync", s);
try {
JSONObject obj = new JSONObject(s);
if (obj.getBoolean("success")) {
String success = obj.getString("message");
//Toast.makeText(getApplicationContext(), success, Toast.LENGTH_LONG).show();
} else {
String failure = obj.getString("message");
//Toast.makeText(getApplicationContext(), failure, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Toast.makeText(getApplicationContext(), "Failed to sync with server", Toast.LENGTH_LONG).show();
Log.d("sqlite sync error", String.valueOf(error));
progbar.setVisibility(View.INVISIBLE);
}
});
}
當我登錄什麼JASON看起來像Android的我得到這種格式:
[{
"zip": "325,
"phone": "78291849",
"uid": "14538177211"
}]
但在我的服務器上,我仍然得到一個空的數組。我究竟做錯了什麼?
這是我的請求格式看起來應該像:
{
"offline":[
{
"zip": "325,
"phone": "78291849",
"uid": "14538177211"
}
]
}
這裏是我收到的要求:
public function massData()
// offline sync
{
$input = Input::all();
return $input;
但目前不在服務器上發送'array'。像'client.addHeader(「data」,loadCheckoutDB.composeJSONfromSQLite());' –
感謝您的早期反應。我試過,我仍然得到我的答覆作爲[],一個空數組。 –
顯示如何在服務器端代碼中訪問'offline' –