在Android

时间:2015-07-21 08:25:17

标签: java android broken-pipe

我将带有标签“pic”的pic文件添加到reqEntity.addpart(),但是如果我使用引用问题中讲述的方法,我该怎么做呢,更多,我使用的是multipart entity jar 。所以这不是引用问题的完全重复。

我在尝试执行以下代码时获得Java.Socketexception: EPIPE:Broken PIPE

class Post_Image extends AsyncTask<String, Void, Void> {

        private String token, response;
        private int response_code;

        public Post_Image(String token, String filePath) {

            this.token = token;
        }

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(String... url) {
            String serverPath = "";
            if (mPostImages.get(counter).getType().equals(AppConstants.PIC_TEAM)
                    || mPostImages.get(counter).getType().equals(AppConstants.VIDEO_TEAM)) {
                serverPath = AppConstants.POST_TEAM_IMAGE + mPostImages.get(counter).getID() + "/";
            } else {
                serverPath = AppConstants.POST_STUDENT_IMAGE + mPostImages.get(counter).getID() + "/";
            }
            String localPath = mPostImages.get(counter).getPath();
            System.out.println(new File(localPath).exists() + ":::::::::::::::esists" + serverPath);
            if (new File(localPath).exists()) {
                response_code = postFile(serverPath, localPath, token);
            }
            // System.out.println("responseCode " + response_code);
            return null;
        }

        private int postFile(String url, String filePath2, String token) {
            int status_code = 0;
            try {

                // new HttpClient
                HttpClient httpClient = new DefaultHttpClient();

                // post header
                HttpPost httpPost = new HttpPost(url);

                ArrayList<NameValuePair> headers = new ArrayList<NameValuePair>();

                headers.add(new BasicNameValuePair("Authorization", "JWT " + token));

                for (NameValuePair h : headers) {
                    httpPost.addHeader(h.getName(), h.getValue());
                }

                File file = new File(filePath2);
                FileBody fileBody = new FileBody(file);

                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("pic", fileBody);
                httpPost.setEntity(reqEntity);

                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity resEntity = response.getEntity();

                status_code = response.getStatusLine().getStatusCode();

                if (resEntity != null) {

                    String responseStr = EntityUtils.toString(resEntity).trim();
                    Log.v("Image upload ", "Response: " + responseStr);
                    System.out.println(status_code + "::::::::::::::Image upload code");

                }

            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return status_code;
        }

        @Override
        protected void onPostExecute(Void result) {

            /*
             * Toast.makeText(getApplicationContext(), "code: " + response_code,
             * Toast.LENGTH_SHORT).show();
             */
            if (response_code == 200) {
                File file = new File(mPostImages.get(counter).getPath());
                if (file.exists()) {
                    file.delete();
                }
                String condition = "id = '" + mPostImages.get(counter).getID() + "' AND " + "mentor_id = '" + mMentorID
                        + "' AND " + "type = '" + mPostImages.get(counter).getType() + "' AND " + "mentor_password = '"
                        + mLoggedUserPwd + "'";
                mDb.deleteRecord(AppConstants.TBL_TEAM_STUDENT_PICS, condition);

            } else {
                // if(progressDialogObj!=null && progressDialogObj.isShowing()){
                // progressDialogObj.dismiss();
                postAll = +1;
                Toast.makeText(getApplicationContext(), mPostImages.get(counter).getPath() + " Not Uploaded.",
                        Toast.LENGTH_SHORT).show();
                // }
            }

            counter += 1;
            if (mPostImages.size() > counter) {
                new Post_Image(mToken, mPostImages.get(counter).getPath()).execute();
            } else {
                if (progressDialogObj != null && progressDialogObj.isShowing()) {
                    progressDialogObj.dismiss();
                    if (postAll > 0) {
                        Toast.makeText(getApplicationContext(), "Posting not completely successfull, please try later.",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Push Successfully.", Toast.LENGTH_SHORT).show();
                    }

                }
            }

            if (response_code == AppConstants.RESPONSE_UNAUTHORIZED
                    || response_code == AppConstants.RESPONSE_INVALID_CREDENTIALS) {
                if (progressDialogObj != null && progressDialogObj.isShowing()) {
                    progressDialogObj.dismiss();
                }
            }

        }

    }

0 个答案:

没有答案