vyxf vyxf - 3 months ago 10
Android Question

unsual- get request result not updated after post request volley

I'm new to both mobile development for android AND php so please go easy on me if my question is not relevant. Thanks.

The requirement is to develop a function to synchronize - upload and then download data, by clicking at button, from an android device sqlite database to mysql server on godaddy platform.


  • I developed 2 seperate webservices: 1 for the upload function and 1 for the download function hosted on godaddy server

  • I used volley's request apis

  • I developed a synchelper class to upload and download function for android.



PROBLEM
- The upload and download functions work fine when they are invoked on click by separate buttons, one after the other. But when I integrate the upload and download functions together into one button, the upload function works fine but the download function does not download the latest image of the mysql database which includes the data that has just been uploaded.

Can anyone shed some light on this?

Here's my code:

In 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);
}


In MainActivity, I call:

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();
}
});

Answer

new Response.Listener<JSONObject>() when uploading sounds like it is asynchronous, so will return immediately back to your click handler event.

If you want to trigger the download after your upload has completed, you need to call it from the onResponse() method in the upload's listener.