List of usage examples for org.apache.http.entity.mime.content StringBody StringBody
public StringBody(final String text) throws UnsupportedEncodingException
From source file:com.ushahidi.android.app.net.MainHttpClient.java
/** * Upload files to server 0 - success, 1 - missing parameter, 2 - invalid * parameter, 3 - post failed, 5 - access denied, 6 - access limited, 7 - no * data, 8 - api disabled, 9 - no task found, 10 - json is wrong *//*from w w w . jav a 2 s.co m*/ public static int PostFileUpload(String URL, HashMap<String, String> params) throws IOException { Log.d(CLASS_TAG, "PostFileUpload(): upload file to server."); entity = new MultipartEntity(); // Dipo Fix try { // wrap try around because this constructor can throw Error final HttpPost httpost = new HttpPost(URL); if (params != null) { entity.addPart("task", new StringBody(params.get("task"))); entity.addPart("incident_title", new StringBody(params.get("incident_title"), Charset.forName("UTF-8"))); entity.addPart("incident_description", new StringBody(params.get("incident_description"), Charset.forName("UTF-8"))); entity.addPart("incident_date", new StringBody(params.get("incident_date"))); entity.addPart("incident_hour", new StringBody(params.get("incident_hour"))); entity.addPart("incident_minute", new StringBody(params.get("incident_minute"))); entity.addPart("incident_ampm", new StringBody(params.get("incident_ampm"))); entity.addPart("incident_category", new StringBody(params.get("incident_category"))); entity.addPart("latitude", new StringBody(params.get("latitude"))); entity.addPart("longitude", new StringBody(params.get("longitude"))); entity.addPart("location_name", new StringBody(params.get("location_name"), Charset.forName("UTF-8"))); entity.addPart("person_first", new StringBody(params.get("person_first"), Charset.forName("UTF-8"))); entity.addPart("person_last", new StringBody(params.get("person_last"), Charset.forName("UTF-8"))); entity.addPart("person_email", new StringBody(params.get("person_email"), Charset.forName("UTF-8"))); if (!TextUtils.isEmpty(params.get("filename"))) { File file = new File(params.get("filename")); if (file.exists()) { entity.addPart("incident_photo[]", new FileBody(new File(params.get("filename")))); } } // NEED THIS NOW TO FIX ERROR 417 httpost.getParams().setBooleanParameter("http.protocol.expect-continue", false); httpost.setEntity(entity); HttpResponse response = httpClient.execute(httpost); Preferences.httpRunning = false; HttpEntity respEntity = response.getEntity(); if (respEntity != null) { InputStream serverInput = respEntity.getContent(); return Util.extractPayloadJSON(GetText(serverInput)); } } } catch (MalformedURLException ex) { Log.d(CLASS_TAG, "PostFileUpload(): MalformedURLException"); ex.printStackTrace(); return 11; // fall through and return false } catch (IllegalArgumentException ex) { Log.e(CLASS_TAG, ex.toString()); //invalid URI return 12; } catch (IOException e) { Log.e(CLASS_TAG, e.toString()); //timeout return 13; } return 10; }
From source file:li.zeitgeist.api.ZeitgeistApi.java
public List<Item> createByFiles(List<File> files, String tags, boolean announce, OnProgressListener listener) throws ZeitgeistError { MultipartEntity entity;/*from w w w .j a va2 s . co m*/ if (listener == null) { entity = new MultipartEntity(); } else { entity = new MultipartEntityWithProgress(listener); } for (File file : files) { entity.addPart("image_upload[]", new FileBody(file)); } try { entity.addPart("tags", new StringBody(tags)); entity.addPart("announce", new StringBody(announce ? "true" : "false")); } catch (UnsupportedEncodingException e) { throw new ZeitgeistError("UnsupportedEncoding: " + e.getMessage()); } Map<String, ?> jsonObject = postRequest("/new", entity); ArrayList<Map<String, ?>> itemObjects = (ArrayList<Map<String, ?>>) jsonObject.get("items"); List<Item> items = new Vector<Item>(); for (Map<String, ?> itemObject : itemObjects) { items.add(new Item(itemObject, baseUrl)); } return items; }
From source file:org.nasa.openspace.gc.geolocation.LocationActivity.java
public void executeMultipartPost() throws Exception { try {/*from w w w . j a va2s. com*/ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://192.168.14.102/index.php/photos/upload"); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Session.image.compress(Bitmap.CompressFormat.PNG, 100, byteStream); byte[] buffer = byteStream.toByteArray(); ByteArrayBody body = new ByteArrayBody(buffer, "profile_image"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("gambar", body); entity.addPart("nama", new StringBody(Session.name)); //POST nama entity.addPart("keterangan", new StringBody(Session.caption)); //POST keterangan entity.addPart("lat", new StringBody(lat)); //POST keterangan entity.addPart("lon", new StringBody(longt)); //POST keterangan post.setEntity(entity); System.out.println("post entity length " + entity.getContentLength()); ResponseHandler handler = new BasicResponseHandler(); String response = client.execute(post, handler); } catch (Exception e) { Log.e(e.getClass().getName(), e.getMessage()); } }
From source file:com.vmware.photon.controller.client.RestClient.java
public HttpResponse upload(String path, String inputFileName, Map<String, String> arguments) throws IOException { HttpClient client = getHttpClient(); HttpPost httppost = new HttpPost(target + path); File file = new File(inputFileName); if (sharedSecret != null) { httppost.addHeader(AUTHORIZATION_HEADER, AUTHORIZATION_METHOD + sharedSecret); }/*ww w . j a v a2 s. c o m*/ MultipartEntity mpEntity = new MultipartEntity(); FileBody cbFile = new FileBody(file, "application/octect-stream"); for (Map.Entry<String, String> argument : arguments.entrySet()) { StringBody stringBody = new StringBody(argument.getValue()); mpEntity.addPart(argument.getKey(), stringBody); } mpEntity.addPart("file", cbFile); httppost.setEntity(mpEntity); return client.execute(httppost); }
From source file:eu.prestoprime.p4gui.connection.AdminConnection.java
public static boolean restoreFromLTO(P4Service service, String from, String to) { try {//ww w . j a v a2 s .c om String path = service.getURL() + "/admin/restore"; HttpRequestBase request = new HttpPost(path); MultipartEntity part = new MultipartEntity(); part.addPart("from", new StringBody(from)); part.addPart("to", new StringBody(to)); ((HttpPost) request).setEntity(part); P4HttpClient client = new P4HttpClient(service.getUserID()); HttpEntity entity = client.executeRequest(request).getEntity(); if (entity != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String line; if ((line = reader.readLine()) != null) { if (line.equals("Error")) { return false; } else { return true; } } } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:org.apache.sling.testing.tools.osgi.WebconsoleClient.java
/** Calls PackageAdmin.refreshPackages to enforce re-wiring of all bundles. */ public void refreshPackages() throws Exception { log.info("Refresh packages."); final MultipartEntity entity = new MultipartEntity(); entity.addPart("action", new StringBody("refreshPackages")); executor.execute(builder.buildPostRequest(CONSOLE_BUNDLES_PATH).withCredentials(username, password) .withEntity(entity)).assertStatus(200); }
From source file:cn.dacas.emmclient.security.ssl.SslHttpStack.java
/** * If Request is MultiPartRequest type, then set MultipartEntity in the httpRequest object. * @param httpRequest//from w w w .ja v a 2 s .c om * @param request * @throws AuthFailureError */ private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { // Return if Request is not MultiPartRequest if (request instanceof MultiPartRequest == false) { return; } MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); //Iterate the fileUploads Map<String, File> fileUpload = ((MultiPartRequest) request).getFileUploads(); for (Map.Entry<String, File> entry : fileUpload.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); multipartEntity.addPart(((String) entry.getKey()), new FileBody((File) entry.getValue())); } //Iterate the stringUploads Map<String, String> stringUpload = ((MultiPartRequest) request).getStringUploads(); for (Map.Entry<String, String> entry : stringUpload.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); try { multipartEntity.addPart(((String) entry.getKey()), new StringBody((String) entry.getValue())); } catch (Exception e) { e.printStackTrace(); } } httpRequest.setEntity(multipartEntity); }
From source file:com.ring.ytjojo.ssl.SslHttpStack.java
/** * If Request is MultiPartRequest type, then set MultipartEntity in the httpRequest object. * @param httpRequest//from w w w . j a v a 2s . co m * @param request * @throws AuthFailureError */ private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { // Return if Request is not MultiPartRequest if (request instanceof MultiPartRequest == false) { return; } MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); //Iterate the fileUploads Map<String, File> fileUpload = ((com.ring.ytjojo.volley.MultiPartRequest) request).getFileUploads(); for (Map.Entry<String, File> entry : fileUpload.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); multipartEntity.addPart(((String) entry.getKey()), new FileBody((File) entry.getValue())); } //Iterate the stringUploads Map<String, String> stringUpload = ((MultiPartRequest) request).getStringUploads(); for (Map.Entry<String, String> entry : stringUpload.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); try { multipartEntity.addPart(((String) entry.getKey()), new StringBody((String) entry.getValue())); } catch (Exception e) { e.printStackTrace(); } } httpRequest.setEntity(multipartEntity); }
From source file:org.semantictools.web.upload.AppspotUploadClient.java
public void upload(String contentType, String path, File file) throws IOException { if (file.getName().equals(CHECKSUM_PROPERTIES)) { return;//from ww w . j av a 2 s . c om } if (!path.startsWith("/")) { path = "/" + path; } // Do not upload if we can confirm that we previously uploaded // the same content. String checksumKey = path.concat(".sha1"); String checksumValue = null; try { checksumValue = Checksum.sha1(file); String prior = checksumProperties.getProperty(checksumKey); if (checksumValue.equals(prior)) { return; } } catch (NoSuchAlgorithmException e) { // Ignore. } logger.debug("uploading... " + path); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(servletURL); post.setHeader("CONTENT-TYPE", "multipart/form-data; boundary=xxxBOUNDARYxxx"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "xxxBOUNDARYxxx", Charset.forName("UTF-8")); FileBody body = new FileBody(file, contentType); entity.addPart(CONTENT_TYPE, new StringBody(contentType)); entity.addPart(PATH, new StringBody(path)); if (version != null) { entity.addPart(VERSION, new StringBody(version)); } entity.addPart(FILE_UPLOAD, body); post.setEntity(entity); String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8"); client.getConnectionManager().shutdown(); if (checksumValue != null) { checksumProperties.put(checksumKey, checksumValue); } }
From source file:org.urbanstew.soundcloudapi.test.RequestTest.java
public final void testUploadFileFromByteArray() throws Exception { byte[] emptywav = { 0x52, 0x49, 0x46, 0x46, 0x7C, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6D, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x44, (byte) 0xAC, 0x00, 0x00, (byte) 0x88, 0x58, 0x01, 0x00, 0x02, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; List<NameValuePair> params = new java.util.ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("track[title]", "This is a test upload")); params.add(new BasicNameValuePair("track[sharing]", "private")); HttpResponse response = mApi.upload(new StringBody(new String(emptywav)), params); assertEquals(201, response.getStatusLine().getStatusCode()); sCreatedTrack2Id = getId(response);/*from w w w . j a va2 s .c o m*/ }