List of usage examples for org.apache.http.entity.mime.content FileBody getMimeType
public String getMimeType()
From source file:com.ad.mediasharing.ADUploadMediaTask.java
@Override protected String doInBackground(Void... params) { String serverResponse = ""; try {/*from w w w . j ava 2 s. c o m*/ // Set timeout parameters int timeout = ADMediaSharingConstants.UPLOAD_REQUEST_TIME_OUT; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeout); HttpConnectionParams.setSoTimeout(httpParameters, timeout); // Initiate connection parts HttpClient client = new DefaultHttpClient(httpParameters); HttpPost postRequest = new HttpPost(mRequest.getRemoteRequestUri()); // Add headers if (!mRequest.getRequestHeaders().isEmpty()) { for (String sKey : mRequest.getRequestHeaders().keySet()) { String sValue = mRequest.getRequestHeaders().get(sKey); postRequest.addHeader(sKey, sValue); } } FileBody media = getMediaType(mRequest.getMediaFile()); postRequest.addHeader("Media-Type", media.getMimeType()); ADCustomMultiPartEntity multipartE = new ADCustomMultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, new ADCustomMultiPartEntity.ProgressListener() { int lastPourcent = 0; @TargetApi(Build.VERSION_CODES.CUPCAKE) @Override public void transferred(long num) { int currentPourcent = (int) ((num / (float) totalSize) * 100); if (currentPourcent > lastPourcent && currentPourcent < 90) { publishProgress(currentPourcent); lastPourcent = currentPourcent; } } }); // Add the post elements multipartE.addPart("file", media); if (mRequest.getPostBodyParams() != null) { for (String sKey : mRequest.getPostBodyParams().keySet()) { String sVal = mRequest.getPostBodyParams().get(sKey); multipartE.addPart(sKey, new StringBody(sVal)); } } totalSize = multipartE.getContentLength(); postRequest.setEntity(multipartE); HttpResponse response = client.execute(postRequest); // Get the response from server HttpEntity theEnt = response.getEntity(); serverResponse = EntityUtils.toString(theEnt); if (ADMediaSharingConstants.DEBUG) { Log.d(TAG, "Result: " + serverResponse); } //Show the remaining progress. mProgressHandler.post(new Runnable() { public void run() { int i = uploadProgress; while (i <= 100) { try { publishProgress(i); i++; } catch (Exception e) { if (ADMediaSharingConstants.DEBUG) Log.d(TAG, e.getMessage()); } } } }); } catch (SocketTimeoutException e) { e.printStackTrace(); serverResponse = "unreachable"; } catch (ConnectTimeoutException e) { e.printStackTrace(); serverResponse = "unreachable"; } catch (Exception e) { e.printStackTrace(); serverResponse = "unreachable"; } return serverResponse; }