List of usage examples for com.squareup.okhttp MultipartBuilder MultipartBuilder
public MultipartBuilder()
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
License:Apache License
@Override public synchronized CFile uploadFile(@NonNull File file, @Nullable CFolder parent) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from w ww . j ava 2 s .c o m // create parameter as json final JSONObject params = new JSONObject(); try { params.put("name", file.getName()); params.put("parent", new JSONObject().put("id", parent != null ? parent.getId() : getRoot().getId())); } 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("attributes", params.toString()) .addFormDataPart("file", file.getName(), RequestBody.create(fileType, file)).build(); Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/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.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"); }/*from w w w .ja v a 2s .c om*/ // 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"); }/*from ww w .ja v a 2s. c o m*/ 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"); }/*from w w w . j a v a 2 s .c om*/ 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 2s . 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")); }//from ww w .j a va 2s. 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. j a v a2 s. co 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_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);//from w w w .j av a 2 s. com 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); } 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 a v a 2s . co m * * @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.//from w w w .ja va2 s.co m * * @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; }