0
我有一个图像上传。如果互联网连接停止,则会显示警告对话框,并询问用户是否要重试或取消上传。问题是,有时当我重试时,在第二次上传后,有两个图像上传而不是一个。可能是什么问题呢?Android HttpClient上传文件错误处理
这里是我的代码:
@Override
protected Void doInBackground(Void... params) {
Log.i("ImageUpload", "Uploading..");
setBitmap();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(url);
httpPostRequest.setHeader("Cookie", this.cookie);
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder
.create();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
this.image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
multiPartEntityBuilder.addBinaryBody("picture", byteArray,
ContentType.create("image/png"), "image.png");
httpPostRequest.setEntity(multiPartEntityBuilder.build());
try {
HttpResponse httpResponse = null;
Log.i("ImageUpload", "====================================================\n"
+ "httpClient.execute(httpPostRequest)");
httpResponse = httpClient.execute(httpPostRequest);
Log.i("ImageUpload", "====================================================");
Log.i("ImageUpload", "httpPost executed");
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
String result;
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
} else {
result = "Unable to send signiture.";
}
Log.i("Upload result", result);
} catch (IOException e) {
photoSent = false;
httpPostRequest.abort();
this.cancel(true);
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
this.mainActivity.hideProgressBar();
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(
mainActivity);
myAlertDialog
.setTitle("Error why uploading the picture.");
myAlertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
myAlertDialog.setPositiveButton("Retry",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*execute the whole AsyncTask again*/
}
});
myAlertDialog.show();
super.onCancelled();
}
MultipartEntity已弃用 – definera 2014-08-28 11:58:48
正如您在我的代码中看到的,我使用MultiparEntityBuilder ...您的答案如何解决我的问题? – definera 2014-08-28 12:07:31
http://stackoverflow.com/q/20284542/1318946 – 2014-08-28 12:11:44