0
我試圖從Android手機上傳多個圖像到服務器(PHP後端),我想要做的是我希望我的函數在返回成功時調用它自己在響應但不同的數據,所以我想移動到我的數組列表中的下一個對象,如果第一個對象成功上傳。我目前正在使用for循環來調用該函數,該循環同時上傳多個圖像,這將消耗3g上的大量連接,因此我更願意一個接一個地上傳它們,並在發生連接失敗或上傳失敗的情況下重試。 請任何一個誰也幫我,我將感謝使用JSON將數據上傳到服務器
這是我的代碼
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);
}
什麼問題?幫你什麼?你有多久了,你有什麼嘗試? – Epodax
我需要一種方式,使函數上傳圖像可以每次使用新數據調用它自己,而不是使用for循環!以便我可以檢測其中一個圖像是否無法上傳 –
使用AsyncTask一次上傳1張圖片,如果成功上傳,則返回成功,然後在for循環中調用它,如果(response == sucess)轉到另一個並且通過相同的AsyncTask上傳它 –