【问题标题】:Android HttpClient Upload File Error HandlingAndroid HttpClient上传文件错误处理
【发布时间】:2023-03-14 21:44:01
【问题描述】:

我有一个图片上传器。如果互联网连接停止,则会显示一个警告对话框并询问用户是否要重试或取消上传。问题是有时当我重试时,在第二次上传后上传了两张图片而不是一张。可能是什么问题?

这是我的代码:

@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();
        }

【问题讨论】:

    标签: android http-post image-uploading androidhttpclient


    【解决方案1】:

    试试这样:

    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();
    

    【讨论】:

    • MultipartEntity 已弃用
    • 正如您在我的代码中看到的,我使用 MultiparEntityBuilder...您的答案如何解决我的问题?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-12-14
      • 1970-01-01
      • 2010-11-17
      • 2017-12-30
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多