이미지 업 로더가 있습니다. 인터넷 연결이 중단되면 경고 대화 상자가 표시되고 사용자에게 다시 시도할지 또는 업로드를 취소할지 묻습니다. 문제는 때로는 다시 시도 할 때 두 번째 업로드 후에 두 개의 이미지가 하나 대신 업로드된다는 것입니다. 무엇이 문제 일 수 있습니까?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
제 코드에서 알 수 있듯이 MultiparEntityBuilder를 사용합니다 ... 어떻게 내 문제를 해결할 수 있습니까? – definera
http://stackoverflow.com/q/20284542/1318946 –