List of usage examples for com.squareup.okhttp MultipartBuilder FORM
MediaType FORM
To view the source code for com.squareup.okhttp MultipartBuilder FORM.
Click Source Link
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
License:Apache License
@Override public synchronized CFile updateFile(@NonNull CFile file, File content) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/* w w w . j a v a2 s .c o m*/ // create multipart body MediaType fileType = MediaType.parse(FilesUtils.getFileType(content)); RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("file", content.getName(), RequestBody.create(fileType, content)).build(); Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/" + file.getId() + "/content") .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { // new file created JSONObject jsonObject = new JSONObject(response.body().string()); JSONArray entries = jsonObject.getJSONArray("entries"); return buildFile(entries.getJSONObject(0)); } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java
License:Apache License
@Override public CFile uploadFile(@NonNull File file, @Nullable CFolder parent) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/* w w w .j a va 2s . c om*/ Uri uri = Uri.parse(mContentUrl); String url = uri.buildUpon().appendEncodedPath("nodes").build().toString(); // create parameter as json final JSONObject params = new JSONObject(); try { params.put("name", file.getName()); params.put("kind", "FILE"); ArrayList<String> parentList = new ArrayList<>(); parentList.add(parent != null ? parent.getId() : getRoot().getId()); params.put("parents", new JSONArray(parentList)); } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } // create multipart body MediaType fileType = MediaType.parse(FilesUtils.getFileType(file)); RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("metadata", params.toString()) .addFormDataPart("content", file.getName(), RequestBody.create(fileType, file)).build(); Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body().string()); return buildFile(jsonObject); } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java
License:Apache License
@Override public CFile updateFile(@NonNull CFile file, File content) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }// w ww . j av a 2 s.c o m Uri uri = Uri.parse(mContentUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString(); // create multipart body MediaType fileType = MediaType.parse(FilesUtils.getFileType(content)); RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("content", file.getName(), RequestBody.create(fileType, content)).build(); Request request = new Request.Builder().url(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(multipart).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(response.body().string()); return buildFile(jsonObject); } else { throw new RequestFailException(response.message(), response.code()); } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.hileone.restretrofit.request.RequestBuilder.java
License:Apache License
RequestBuilder(String method, HttpUrl baseUrl, String relativeUrl, Headers headers, MediaType contentType, boolean hasBody, boolean isFormEncoded, boolean isMultipart) { this.method = method; this.baseUrl = baseUrl; this.relativeUrl = relativeUrl; this.requestBuilder = new Request.Builder(); this.contentType = contentType; this.hasBody = hasBody; if (headers != null) { requestBuilder.headers(headers); }//from w w w .j av a 2 s .c o m if (isFormEncoded) { // Will be set to 'body' in 'build'. formEncodingBuilder = new FormEncodingBuilder(); } else if (isMultipart) { // Will be set to 'body' in 'build'. multipartBuilder = new MultipartBuilder(); multipartBuilder.type(MultipartBuilder.FORM); } }
From source file:com.hippo.nimingban.client.ac.ACEngine.java
License:Apache License
public static Call prepareReply(OkHttpClient okHttpClient, ACReplyStruct struct) throws Exception { MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"name\""), RequestBody.create(null, StringUtils.avoidNull(struct.name))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"email\""), RequestBody.create(null, StringUtils.avoidNull(struct.email))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, StringUtils.avoidNull(struct.title))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"content\""), RequestBody.create(null, StringUtils.avoidNull(struct.content))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"resto\""), RequestBody.create(null, StringUtils.avoidNull(struct.resto))); if (struct.water) { builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"water\""), RequestBody.create(null, "true")); }//w w w. ja v a2 s . c o m InputStreamPipe isPipe = struct.image; if (isPipe != null) { String filename; MediaType mediaType; byte[] bytes; File file = compressBitmap(isPipe, struct.imageType); if (file == null) { String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(struct.imageType); if (TextUtils.isEmpty(extension)) { extension = "jpg"; } filename = "a." + extension; mediaType = MediaType.parse(struct.imageType); if (mediaType == null) { mediaType = MEDIA_TYPE_IMAGE_ALL; } try { isPipe.obtain(); bytes = IOUtils.getAllByte(isPipe.open()); } finally { isPipe.close(); isPipe.release(); } } else { filename = "a.jpg"; mediaType = MEDIA_TYPE_IMAGE_JPEG; InputStream is = null; try { is = new FileInputStream(file); bytes = IOUtils.getAllByte(is); } finally { IOUtils.closeQuietly(is); file.delete(); } } builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"" + filename + "\""), RequestBody.create(mediaType, bytes)); } String url = ACUrl.API_REPLY; Log.d(TAG, url); Request request = new GoodRequestBuilder(url).post(builder.build()).build(); return okHttpClient.newCall(request); }
From source file:com.hippo.nimingban.client.ac.ACEngine.java
License:Apache License
public static Call prepareCreatePost(OkHttpClient okHttpClient, ACPostStruct struct) throws Exception { MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"name\""), RequestBody.create(null, StringUtils.avoidNull(struct.name))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"email\""), RequestBody.create(null, StringUtils.avoidNull(struct.email))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, StringUtils.avoidNull(struct.title))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"content\""), RequestBody.create(null, StringUtils.avoidNull(struct.content))); builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"fid\""), RequestBody.create(null, StringUtils.avoidNull(struct.fid))); if (struct.water) { builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"water\""), RequestBody.create(null, "true")); }//from w w w . java2 s. com InputStreamPipe isPipe = struct.image; if (isPipe != null) { String filename; MediaType mediaType; byte[] bytes; File file = compressBitmap(isPipe, struct.imageType); if (file == null) { String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(struct.imageType); if (TextUtils.isEmpty(extension)) { extension = "jpg"; } filename = "a." + extension; mediaType = MediaType.parse(struct.imageType); if (mediaType == null) { mediaType = MEDIA_TYPE_IMAGE_ALL; } try { isPipe.obtain(); bytes = IOUtils.getAllByte(isPipe.open()); } finally { isPipe.close(); isPipe.release(); } } else { filename = "a.jpg"; mediaType = MEDIA_TYPE_IMAGE_JPEG; InputStream is = null; try { is = new FileInputStream(file); bytes = IOUtils.getAllByte(is); } finally { IOUtils.closeQuietly(is); file.delete(); } } builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"" + filename + "\""), RequestBody.create(mediaType, bytes)); } String url = ACUrl.API_CREATE_POST; Log.d(TAG, url); Request request = new GoodRequestBuilder(url).post(builder.build()).build(); return okHttpClient.newCall(request); }
From source file:com.hkm.root.Tasks.upload_data.java
License:Open Source License
public void OCokHttpUpload(ArrayList<Uri> img_list, final String endpoint, final MediaType typ) throws IOException, Exception { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image MultipartBuilder mb = new MultipartBuilder(); mb.type(MultipartBuilder.FORM); for (Uri i : img_list) { final File file_location = i.toString().startsWith("file:") ? new File(i.toString().replace("file:///", "")) : new File(Tool.getRealPathFromURI(ac, i)); final RequestBody content_data = RequestBody.create(typ, file_location); final String disposition = String.format(Locale.getDefault(), "form-data; name=\"%s\"; filename=\"%s\"", "target-" + post_target, file_location.getName()); final String disposition_alt = String.format(Locale.getDefault(), "file; filename=\"%s\"", file_location.getName()); mb.addPart(Headers.of("Content-Disposition", disposition_alt, "Content-Transfer-Encoding", "binary"), content_data);/*w ww. ja v a 2 s .c o m*/ } RequestBody requestBody = mb.build(); Request requestBuild = new Request.Builder().url(endpoint + "?pid=" + job_id + "&target=" + post_target) .post(requestBody).tag("post_image").build(); execute_upload(requestBuild); }
From source file:com.ibm.watson.developer_cloud.dialog.v1.DialogService.java
License:Open Source License
/** * Creates a dialog.//from w ww . j av a2s .c om * * @param name The dialog name * @param dialogFile The dialog file created by using the Dialog service Applet. * @return The created dialog * @see Dialog */ public Dialog createDialog(final String name, final File dialogFile) { if (name == null || name.isEmpty()) throw new IllegalArgumentException("name cannot be null or empty"); if (dialogFile == null || !dialogFile.exists()) throw new IllegalArgumentException("dialogFile cannot be null or empty"); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile)) .addFormDataPart(NAME, name).build(); final Request request = RequestBuilder.post(PATH_DIALOGS).withBody(body).build(); return executeRequest(request, Dialog.class); }
From source file:com.ibm.watson.developer_cloud.dialog.v1.DialogService.java
License:Open Source License
/** * Updates a dialog.// www . jav a2 s. c om * * @param dialogId The dialog identifier * @param dialogFile The dialog file * @return the created dialog * @see Dialog */ public Dialog updateDialog(final String dialogId, final File dialogFile) { if (dialogId == null || dialogId.isEmpty()) throw new IllegalArgumentException("dialogId cannot be null or empty"); if (dialogFile == null || !dialogFile.exists()) throw new IllegalArgumentException("dialogFile cannot be null or empty"); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile)).build(); final Request request = RequestBuilder.put(String.format(PATH_DIALOG, dialogId)).withBody(body).build(); executeWithoutResponse(request); final Dialog dialog = new Dialog().withDialogId(dialogId); return dialog; }
From source file:com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion.java
License:Open Source License
/** * Converts a document and returns an {@link InputStream}. * //from ww w .j av a 2 s .c o m * @param document The file to convert * @param mediaType Internet media type of the file * @param conversionTarget The conversion target to use * @param customConfig The additional config params to use * @return Converted document in the specified format * @see {@link HttpMediaType} for available media types */ private InputStream convertDocument(final File document, final String mediaType, final ConversionTarget conversionTarget, final JsonObject customConfig) { if (document == null || !document.exists()) throw new IllegalArgumentException("document cannot be null and must exist"); if (customConfig == null) throw new NullPointerException("custom config must not be null"); final String type = mediaType != null ? mediaType : ConversionUtils.getMediaTypeFromFile(document); if (type == null) { throw new RuntimeException("mediaType cannot be null or empty"); } else if (!ConversionUtils.isValidMediaType(type)) { throw new IllegalArgumentException("file with the given media type is not supported"); } final JsonObject configJson = new JsonObject(); // Do this since we shouldn't mutate customConfig for (Map.Entry<String, JsonElement> entry : customConfig.entrySet()) { configJson.add(entry.getKey(), entry.getValue()); } // Add or override the conversion target configJson.addProperty(CONVERSION_TARGET, conversionTarget.toString()); final MediaType mType = MediaType.parse(type); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"config\""), RequestBody.create(HttpMediaType.JSON, configJson.toString())) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"file\""), RequestBody.create(mType, document)) .build(); final Request request = RequestBuilder.post(CONVERT_DOCUMENT_PATH).withQuery(VERSION, versionDate) .withBody(body).build(); final Response response = execute(request); return ResponseUtil.getInputStream(response); }