我正在尝试从Android手机上传多个图像到服务器(PHP后端),我正在尝试做的是我希望我的功能在响应时返回成功但是使用不同的数据时将其称为自身,所以我想移动到我的数组列表中的下一个对象,如果第一个对象上传成功。我正在调用使用for循环的函数,它同时上传多个图像,这将消耗3g上的大量连接,所以我更喜欢逐个上传它们并重试以防万一我丢失连接或无法上传。 请任何能帮助我的人我会感激
这是我的代码
private void uploaddata() {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("uploading to server ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_Uploadfirstparty, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String accidentid="";
if(acc.getAccid()==null)
{
accidentid = jObj.getString("recordid");
Toast.makeText(getApplicationContext(),
"accident"+accidentid, Toast.LENGTH_LONG).show();
}
else
{
accidentid = acc.getAccid();
}
String thid="0";
for(int i=0;i<10;i++)
{
if(acc.p[i]!=null)
{
uploadimages(imgstring(acc.p[i].getPhoto()),acc.p[i].getPosition(),Integer.parseInt(accidentid),acc.p[i].getComment(),thid);
}
}
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("carid", Integer.toString(acc.getCarid()));
params.put("expertid",Integer.toString(acc.getExpertid()));
params.put("longtitude",Double.toString(acc.getLongtitude()));
params.put("latitude",Double.toString(acc.getLatitude()));
params.put("injuries",Integer.toString(acc.getInjuries()));
params.put("location",acc.getLocation());
params.put("time",acc.getTime());
params.put("accid",acc.getAccid());
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
strReq.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
private void uploadimage(final String image,final String thid,final int pos,final int id) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("uploading images to server ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_Uploadfirstpartyphoto, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String noerror = jObj.getString("noerror");
Toast.makeText(getApplicationContext(),
noerror, Toast.LENGTH_LONG).show();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("image",image);
params.put("pos",Integer.toString(pos));
params.put("thid",thid);
params.put("accidentid",Integer.toString(id));
return params;
}
};
strReq.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
答案 0 :(得分:0)
我设法找到了一个使用递归的解决方案!
if(previousScore > PlayerPrefs.GetInt("LevelHardScore"))
{
PlayerPrefs.SetInt("LevelHardScore" , previousScore);
PlayerPrefs.Save ();
}