2014-08-28 123 views
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(); 
     } 

回答

0

我发现这个问题。我认为问题在于在发送图片之后但在请求结束之前连接丢失。

0

尝试这样的:

InputStream inputStream; 
inputStream = new FileInputStream(new File(photoFile)); 
byte[] data; 
data = IOUtils.toByteArray(inputStream); 

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, 
System.getProperty("http.agent")); 

InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "Pic.jpg"); 

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); 
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
multipartEntity.addPart("PhotoMessage", inputStreamBody); 

httpPost.setEntity(multipartEntity.build()); 

HttpResponse httpResponse = httpClient.execute(httpPost); 
HttpEntity httpEntity = httpResponse.getEntity(); 
is = httpEntity.getContent(); 
+0

MultipartEntity已弃用 – definera 2014-08-28 11:58:48

+0

正如您在我的代码中看到的,我使用MultiparEntityBuilder ...您的答案如何解决我的问题? – definera 2014-08-28 12:07:31

+0

http://stackoverflow.com/q/20284542/1318946 – 2014-08-28 12:11:44