2014-08-28 3 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

+0

제 코드에서 알 수 있듯이 MultiparEntityBuilder를 사용합니다 ... 어떻게 내 문제를 해결할 수 있습니까? – definera

+0

http://stackoverflow.com/q/20284542/1318946 –