2016-01-28 223 views
2

我想發送我的SQLite數據與在線MySQL服務器,但無濟於事。當然,我跑到谷歌,很幸運地找到this。顯然,它應該工作,但它沒有,但我沒有收到我的服務器上的數據。如何將Android的SQLite數據發送到MySQL服務器?

我知道這個問題已被要求herehere,但我還沒有能夠使用給出的建議修補它。

這是我試過的。這就是我使用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; 
+1

但目前不在服務器上發送'array'。像'client.addHeader(「data」,loadCheckoutDB.composeJSONfromSQLite());' –

+0

感謝您的早期反應。我試過,我仍然得到我的答覆作爲[],一個空數組。 –

+0

顯示如何在服務器端代碼中訪問'offline' –

回答

0

這裏是如何我都做到了:

public void syncSQLiteMySQLDB() { 

    //i get my json string from sqlite, see the code i posted above about this 
     final String json = loadCheckoutDB.composeJSONfromSQLite(); 

     new Thread() { 
      public void run() { 
       makeRequest("http://myexample/offline/api", json); 
      } 
     }.start(); 

    } 

    public void makeRequest(String uri, String json) { 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(uri); 
      httpPost.setEntity(new StringEntity(json)); 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("session_id", getapikey()); 
      httpPost.setHeader("Content-type", "application/json"); 
      HttpResponse response = client.execute(httpPost); 
      if (response != null) { 

       String responseBody = EntityUtils.toString(response.getEntity()); 
       Log.d("response to sync", responseBody); 
       Object jsonObj = new JSONTokener(responseBody).nextValue(); 
       if (jsonObj instanceof JSONObject) { 
        JSONObject jsonObject = (JSONObject) jsonObj; 
        //further actions on jsonObjects 

       } else if (jsonObj instanceof JSONArray) { 
        //further actions on jsonArray 
        JSONArray jsonArray = (JSONArray) jsonObj; 
       } 
      } 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

從記錄了很多回復我發現我沒有使用我以前的方法發送內容類型。試過這段代碼後,它就起作用了。

+0

是否有任何可能的方法來實現這個使用齊射? – shaiToro

+0

@shaiToro使用一個庫去掉所有自己寫的不安全的樣板文件。退房okhttp或凌空 – rafid059

+0

@shaiToro我試圖使用凌空,但無法獲得應用程序/ JSON的內容類型。不知道爲什麼。 –

1

您的列表添加到地圖,關鍵是offline和值表:

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()); 
     } 
    HashMap<String, ArrayList<HashMap<String, String>>> offlineMap = new HashMap<String, ArrayList<HashMap<String, String>>>(); 
    offlineMap.put("offline", offlineList); 
    database.close(); 
    Gson gson = new GsonBuilder().create(); 
    //Use GSON to serialize Array List to JSON 
    return gson.toJson(offlineMap); 
} 
相關問題