我需要显示一个凌空的加载屏幕,我使用齐射将少量json发送到服务器,我在里面编写了代码,用于获取json并发送json。但for循环不等待响应继续。它只是快速完成,我如何使用for循环发送jason并显示for循环值作为同步计数。?
...代码
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
Log.d("length", String.valueOf(result.length()));
if(result.length()!=0) {
for (int e = 0; e < result.length(); e++) {
this.dialog.setMessage("Syncing "+(e+1)+" of "+result.length());
this.dialog.show();
try {
final JSONObject js = result.getJSONObject(e);
Log.d("sdfhd", js.toString());
final JSONArray ja = new JSONArray();
ja.put(js);
Log.d("sending json", ja.toString());
JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST,
server_url, ja,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("asd", ja.toString());
Log.d("JsonArray", response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject store = response.getJSONObject(i);
mydb.updateweb(store.getInt("_id"), store.getString("webid"), store.getString("regid"));
if (i == response.length() - 1) {
Toast.makeText(MainActivity.this, "Synced Successfully", Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
dialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
JSONObject stor = response.getJSONObject(0);
Toast.makeText(MainActivity.this, stor.getString("result"),
Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
// dialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Json sdfsdfsf", js.toString());
Log.d("afs", error.toString());
Toast.makeText(MainActivity.this, "error",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}
};
request.setRetryPolicy(new DefaultRetryPolicy(50000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(request);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
else{
Toast.makeText(MainActivity.this, "Updated Succesfully",
Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
dialog.dismiss();
}
// dialog.dismiss();
}
答案 0 :(得分:0)
查看我已经更改了关于对话框的逻辑的代码,我还添加了成功和失败计数功能,以便您可以了解请求状态
全局变量:
int successCount = 0;
int failedCount = 0;
修改了onPostExecute():
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
successCount = 0;
failedCount = 0;
this.dialog.setMessage("Syncing 0 of "+result.length());
this.dialog.show();
Log.d("length", String.valueOf(result.length()));
if(result.length()!=0) {
for (int e = 0; e < result.length(); e++) {
this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());
try {
final JSONObject js = result.getJSONObject(e);
Log.d("sdfhd", js.toString());
final JSONArray ja = new JSONArray();
ja.put(js);
Log.d("sending json", ja.toString());
JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST,
server_url, ja,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
boolean isCodeExecutedSuccessfully = false;
Log.d("asd", ja.toString());
Log.d("JsonArray", response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject store = response.getJSONObject(i);
mydb.updateweb(store.getInt("_id"), store.getString("webid"), store.getString("regid"));
if (i == response.length() - 1) {
Toast.makeText(MainActivity.this, "Synced Successfully", Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
dialog.dismiss();
}
isCodeExecutedSuccessfully = true;
} catch (JSONException e) {
isCodeExecutedSuccessfully = false;
e.printStackTrace();
}
}
try {
JSONObject stor = response.getJSONObject(0);
Toast.makeText(MainActivity.this, stor.getString("result"),
Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
// dialog.dismiss();
isCodeExecutedSuccessfully = true;
} catch (JSONException e) {
isCodeExecutedSuccessfully = false;
e.printStackTrace();
}
//////////////////////////////// DIALOG WORK ON SUCCESS ///////////////////////////////
if(isCodeExecutedSuccessfully)
++successCount;
else
++failedCount;
if((e+1) == result.length()) {
dialog.dismiss();
}
this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());
Log.d("::onResponse(): Status Counts", "Success=" + successCount + ", fail=" + failedCount);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//////////////////////////////// DIALOG WORK ON ERROR RESPONSE ///////////////////////////////
++failedCount;
if((e+1) == result.length()) {
dialog.dismiss();
}
Log.d("::onErrorResponse(): Status Counts", "Success=" + successCount + ", fail=" + failedCount);
this.dialog.setMessage("Syncing " + (e+1) + " of "+result.length());
Log.d("Json sdfsdfsf", js.toString());
Log.d("afs", error.toString());
Toast.makeText(MainActivity.this, "error",
Toast.LENGTH_SHORT).show();
//dialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}
};
request.setRetryPolicy(new DefaultRetryPolicy(50000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(request);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
else{
Toast.makeText(MainActivity.this, "Updated Succesfully",
Toast.LENGTH_SHORT).show();
mydb.updatesync();
mydb.deletet2();
dialog.dismiss();
}
// dialog.dismiss();
}