0
我是新的android和php的移動開發,所以請容易對我,如果我的問題是不相關的。謝謝。unsual-get請求結果沒有更新後發佈請求凌空
需求是開發一個功能來同步 - 通過點擊按鈕,從android設備的sqlite數據庫上傳數據到godaddy平臺上的mysql服務器。
- 我公司開發的2個獨立的web服務:1的上傳功能,1個用於下載功能託管GoDaddy的服務器
- 我用凌空的請求的API
- 我開發了一個synchelper類上傳和下載功能,Android版。
問題 - 上傳和下載功能,做工精細,當他們被單獨的按鈕,一前一後上點擊調用。但是,當我將上傳和下載功能集成到一個按鈕時,上傳功能可以正常工作,但下載功能不會下載包含剛剛上傳的數據的mysql數據庫的最新映像。
誰能一些線索呢?
這裏是我的代碼:
在syncHelper.java
String read_animal_url = "http://www.careagriculture.com/get_all_animals3.php";
String insert_animals_url = "http://www.careagriculture.com/insert_animals25.php";
public void DeviceDataUpload() {
StringRequest postRequest = new StringRequest(Method.POST, insert_animals_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
if (!(response == null)) {
JSONObject c = new JSONObject(response);
System.out.print(insert_animals_url + " response " + response.toString());
int success = c.getInt("success");
if (success == 1) {
localDBHelper.deleteAllAnimalFromDevice();
}
}
}catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Map<String, String> getParams(){
Map<String, String> params = localDBHelper.getAllAnimalMap();
return params;
}
@Override
public Priority getPriority() {
return Priority.HIGH;
}
};
AppController.getInstance().addToReqQueue(postRequest);
}
public void ServerDataDownload(){
JsonObjectRequest readAnimalReq = new JsonObjectRequest(Method.GET, read_animal_url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.print(read_animal_url+" response "+response.toString());
int success = response.getInt("success");
if (success == 1) {
JSONArray ja = response.getJSONArray("animal");
localDBHelper.deleteAllAnimalFromServer();
for (int i = 0; i < ja.length(); i++) {
map.put("ANIMAL_ID", c.getString(TAG_ANIMAL_ID));
map.put("SUPERVISOR", c.getString(TAG_SUPERVISOR));
map.put("ANIMAL_TYPE", c.getString(TAG_ANIMAL_TYPE));
map.put("RECORD_TYPE", "S");
if (!c.isNull(TAG_GENDER))
map.put("GENDER", c.getString(TAG_GENDER));
if (!c.isNull(TAG_DATE_OF_BIRTH))
map.put("DATE_OF_BIRTH", c.getString(TAG_DATE_OF_BIRTH));
if (!c.isNull(TAG_DATE_PURCHASED))
map.put("DATE_PURCHASED", c.getString(TAG_DATE_PURCHASED));
if (!c.isNull(TAG_PURCHASE_PRICE))
map.put("PURCHASE_PRICE", c.getString(TAG_PURCHASE_PRICE));
if (!c.isNull(TAG_DATE_DISTRIBUTED))
map.put("DATE_DISTRIBUTED", c.getString(TAG_DATE_DISTRIBUTED));
if (!c.isNull(TAG_DATE_SOLD))
map.put("DATE_SOLD", c.getString(TAG_DATE_SOLD));
if (!c.isNull(TAG_SALE_PRICE))
map.put("SALE_PRICE", c.getString(TAG_SALE_PRICE));
Item_List.add(map);
localDBHelper.insertAnimalFromServer(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Priority getPriority() {
return Priority.LOW;
}
};
AppController.getInstance().addToReqQueue(readAnimalReq);
}
在MainActivity,我呼籲:
syncButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (localDBHelper.getAllAnimalCount()>0) {
SyncHelper syncHelper = new SyncHelper(MainActivity.this);
syncHelper.DeviceDataUpload();
Toast.makeText(getApplicationContext(),"Upload Completed", Toast.LENGTH_LONG).show();
}
syncHelper.ServerDataDownload();
Toast.makeText(getApplicationContext(),"Sync Completed", Toast.LENGTH_LONG).show();
}
});
謝謝謝謝謝謝你!這是令人難以置信的,它的工作。並且非常感謝你解釋這種行爲! – vyxf