List of usage examples for com.squareup.okhttp MultipartBuilder build
public RequestBody build()
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 . j a v a2 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_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 . jav a 2s . 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);//w w w. ja v a 2 s . c o m 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.visual_recognition.v1.VisualRecognition.java
License:Open Source License
public VisualRecognitionImages recognize(final String imageName, final InputStream image, final LabelSet labelSet) { if (image == null) throw new IllegalArgumentException("image cannot be null"); final RequestBuilder requestBuilder = RequestBuilder.post("/v1/tag/recognize"); final MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); bodyBuilder.addFormDataPart(IMG_FILE, imageName, InputStreamRequestBody.create(HttpMediaType.BINARY_FILE, image)); if (labelSet != null) bodyBuilder.addFormDataPart(LABELS_TO_CHECK, GsonSingleton.getGson().toJson(labelSet)); requestBuilder.withBody(bodyBuilder.build()); return executeRequest(requestBuilder.build(), VisualRecognitionImages.class); }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v2.VisualRecognition.java
License:Open Source License
/** * Classifies the image/s against the Classifiers. The response includes a score for a classifier * only if the score meets the minimum threshold of 0.5. If no score meets the threshold for an * image, no classifiers are returned.//from w w w. j a v a 2s. c o m * * <br> * <br> * Here is an example of how to classify an InputStream image with an existing classifier: * * <pre> * VisualRecognition service = new VisualRecognition("2015-12-02"); * service.setUsernameAndPassword("<username>", "<password>"); * * FileInputStream image = new FileInputStream("car.png"); * VisualClassifier car = new VisualClassifier("Car"); * * VisualClassification result = service.classify("foo.png", image, car); * System.out.println(result); * </pre> * * @param filename The file name * @param imagesInputStream the image/s input stream to classify * @param classifiers the classifiers * @return the {@link Classification} */ public VisualClassification classify(final String filename, final InputStream imagesInputStream, final VisualClassifier... classifiers) { Validate.notNull(imagesInputStream, "image cannot be null"); Validate.notNull(filename, "filename cannot be null"); MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); bodyBuilder.addFormDataPart(IMAGES_FILE, filename, InputStreamRequestBody.create(HttpMediaType.BINARY_FILE, imagesInputStream)); if (classifiers != null && classifiers.length > 0 && classifiers[0] != null) { JsonObject classifierIds = getClassifierIdsAsJson(classifiers); bodyBuilder.addFormDataPart(CLASSIFIER_IDS, classifierIds.toString()); } RequestBuilder requestBuilder = RequestBuilder.post(PATH_CLASSIFY).withQuery(VERSION, versionDate) .withBody(bodyBuilder.build()); return executeRequest(requestBuilder.build(), VisualClassification.class); }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
License:Open Source License
private RequestBody buildParts(BaseRequest request) { MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) { if (h.getFileName() == null) { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), null, RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getBodyAsString()); }//from w ww .j av a 2s . co m } else { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(null, h.getBody())); } } } return multipartBuilder.build(); }
From source file:com.lidroid.xutils.HttpUtils.java
License:Apache License
@NonNull private RequestBody buildFileRequestBody(RequestParams params, HashMap<String, ContentBody> fileParams) { MultipartBuilder builder = new MultipartBuilder(); builder = builder.type(MultipartBuilder.FORM); for (String fileName : fileParams.keySet()) { builder.addFormDataPart(fileName, fileName, RequestBody.create( MediaType.parse("application/octet-stream"), ((FileBody) fileParams.get(fileName)).getFile())); }//from ww w.j a va 2s. c o m List<NameValuePair> bodyParams = params.getBodyParams(); if (bodyParams != null) { for (NameValuePair param : params.getBodyParams()) { builder.addFormDataPart(param.getName(), param.getValue()); } } return builder.build(); }
From source file:com.liferay.mobile.android.http.client.OkHttpClientImpl.java
License:Open Source License
protected RequestBody getUploadBody(Request request) { JSONObject body = (JSONObject) request.getBody(); Object tag = request.getTag(); MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM); Iterator<String> it = body.keys(); while (it.hasNext()) { String key = it.next();/* w ww . j a v a2 s. c om*/ Object value = body.opt(key); if (value instanceof UploadData) { UploadData data = (UploadData) value; RequestBody requestBody = new InputStreamBody(data, tag); builder.addFormDataPart(key, data.getFileName(), requestBody); } else { builder.addFormDataPart(key, value.toString()); } } return builder.build(); }
From source file:com.wialon.remote.OkSdkHttpClient.java
License:Apache License
@Override public void postFile(String url, Map<String, String> params, Callback callback, int timeout, File file) { //Method will work at 2.1 version of library https://github.com/square/okhttp/issues/963 and https://github.com/square/okhttp/pull/969 MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); RequestBody paramsBody = paramsMapToRequestBody(params); if (paramsBody != null) { builder.addPart(paramsBody);//from www. jav a 2 s . com } builder.addPart(RequestBody.create(MediaType.parse(""), file)); Request request = new Request.Builder().url(url).post(builder.build()).build(); threadPool.submit(new HttpRequest(getHttpClient(timeout), request, callback)); }
From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java
License:Apache License
protected RequestBody createMultipartBody(StrParam[] stringStrParams, IOParam[] IOParams) { MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); builder = buildMultipartBody(builder); if (stringStrParams != null && stringStrParams.length > 0) { for (StrParam strParam : stringStrParams) { if (strParam.key != null && strParam.value != null) { builder.addFormDataPart(strParam.key, strParam.value); log("buildMultiStringParam: key: " + strParam.key + " value: " + strParam.value); } else { log("buildMultiStringParam: key: " + (strParam.key != null ? strParam.key : "null") + " value: " + (strParam.value != null ? strParam.value : "null")); }/*from w w w . j a v a 2 s . c om*/ } } if (IOParams != null && IOParams.length > 0) { for (IOParam param : IOParams) { if (param.key != null && param.file != null) { String fileName = param.file.getName(); RequestBody fileBody = RequestBody.create(MediaType.parse(Util.getFileMimeType(fileName)), param.file); builder.addFormDataPart(param.key, fileName, fileBody); log("buildMultiFileParam: key: " + param.key + " value: " + fileName); } else { log("buildMultiFileParam: key: " + (param.key != null ? param.key : "null") + " file: " + (param.file != null ? param.file.getName() : "null")); } } } return builder.build(); }