List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:com.magnet.max.android.Attachment.java
License:Apache License
/** * Upload the attachment to Max Server//from ww w . j a v a2 s .c om * @param listener */ public void upload(final UploadListener listener) { if (StringUtil.isNotEmpty(mAttachmentId)) { // Already uploaded Log.d(TAG, "Already uploaded"); if (null != listener) { listener.onComplete(this); } return; } if (mStatus == Status.INIT || mStatus == Status.ERROR) { if (null != listener) { listener.onStart(this); } final AtomicReference<Long> startTime = new AtomicReference<>(); Callback<Map<String, String>> uploadCallback = new Callback<Map<String, String>>() { @Override public void onResponse(Response<Map<String, String>> response) { if (response.isSuccess()) { Map<String, String> result = response.body(); if (null != result && !result.isEmpty()) { mAttachmentId = result.values().iterator().next(); mStatus = Status.COMPLETE; Log.d(TAG, "It took " + (System.currentTimeMillis() - startTime.get()) / 1000 + " seconds to upload attachment " + mAttachmentId); if (null != listener) { listener.onComplete(Attachment.this); } } else { handleError(new Exception("Can't get attachmentId from response")); } } else { handleError(new Exception(response.message())); } } @Override public void onFailure(Throwable throwable) { handleError(throwable); } private void handleError(Throwable throwable) { mStatus = Status.ERROR; Log.d(TAG, "Failed to upload attachment " + mName, throwable); if (null != listener) { listener.onError(Attachment.this, throwable); } } }; RequestBody requestBody = null; if (mSourceType == ContentSourceType.FILE) { requestBody = RequestBody.create(MediaType.parse(getMimeType()), (File) mContent); } else { requestBody = RequestBody.create(MediaType.parse(getMimeType()), getAsBytes()); } startTime.set(System.currentTimeMillis()); String partName = StringUtil.isNotEmpty(mName) ? mName : "attachment"; getAttachmentService() .uploadMultiple(mMetaData, new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart(partName, partName, requestBody).build(), uploadCallback) .executeInBackground(); mStatus = Status.TRANSFERING; } else if (mStatus == Status.TRANSFERING) { if (null != listener) { listener.onError(this, new IllegalStateException("Attachment is being uploading")); } } }
From source file:com.magnet.max.android.logging.remote.LogFileManager.java
License:Apache License
public static synchronized void uploadFile(final File file, final ApiCallback<Boolean> callback) { if (isCurrentFile(file.getName())) { rotateFile();//from w w w.j a v a 2 s.c o m } RequestBody fileBody = RequestBody.create(MediaType.parse("text/plain"), file); MagnetCall<Void> call = getLogCollectorService().addEventsFromFile(fileBody, new Callback<Void>() { @Override public void onResponse(retrofit.Response<Void> response) { if (response.isSuccess()) { Log.d(TAG, "Success uploading file.."); file.delete(); if (null != callback) { callback.success(true); } } else { if (null != callback) { Log.d(TAG, "Uploading file failed due to : " + response.message()); callback.failure(new ApiError(response.message(), response.code())); } } } @Override public void onFailure(Throwable throwable) { Log.e(TAG, "Error uploading file.." + throwable.getMessage()); if (null != callback) { callback.failure(new ApiError(throwable)); } } }); call.executeInBackground(); }
From source file:com.magnet.max.android.rest.marshalling.MagnetGsonConverter.java
License:Apache License
@Override public RequestBody toBody(T value) { MediaType mediaType = null;/*from w w w .j a v a 2 s. com*/ if (GsonDecorator.getInstance().isBasicType(typeToken.getType())) { mediaType = MEDIA_TYPE_TEXT; } else if (GsonDecorator.getInstance().isDateType(typeToken.getType()) || GsonDecorator.getInstance().isEnum(typeToken.getType())) { mediaType = MEDIA_TYPE_JSON; } else if (value instanceof RequestBody) { return (RequestBody) value; } else { mediaType = MEDIA_TYPE_JSON; //Buffer buffer = new Buffer(); //Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); //try { // typeAdapter.toJson(writer, value); // writer.flush(); // json = buffer.readUtf8(); //} catch (IOException e) { // throw new AssertionError(e); // Writing to Buffer does no I/O. //} } return RequestBody.create(mediaType, GsonDecorator.getInstance().toJson(value)); }
From source file:com.magnet.max.android.rest.qos.internal.CachedRequest.java
License:Apache License
public Request toRequest() { Headers responseHeaders = Headers.of(headers); String contentType = responseHeaders.get("Content-Type"); return new Request.Builder().url(url).method(method, RequestBody.create(MediaType.parse(contentType), body)) .headers(responseHeaders).build(); }
From source file:com.magnet.max.android.tests.EventsCollectionTestLog.java
License:Apache License
private void testUploadLogFile(LogEventsCollectionService myService) { EventLog log = new EventLog.LogBuilder().name("logtest").category("test").message("test").build(); Gson gson = new Gson(); final RequestBody fileBody = RequestBody.create(MediaType.parse("text/plain"), gson.toJson(log)); final CountDownLatch signal = new CountDownLatch(1); myService.addEventsFromFile(fileBody, new retrofit.Callback<Void>() { @Override/*from ww w . j av a 2 s .c o m*/ public void onResponse(retrofit.Response<Void> response) { signal.countDown(); } @Override public void onFailure(Throwable throwable) { fail(throwable.getMessage()); signal.countDown(); } }).executeInBackground(); try { signal.await(10, TimeUnit.SECONDS); } catch (Throwable e) { Log.e(TAG, "addEventsFromFile timeout"); fail("addEventsFromFile timeout"); } assertEquals(0, signal.getCount()); }
From source file:com.mcxiaoke.next.http.NextRequest.java
License:Apache License
protected RequestBody getRequestBody() throws IOException { if (!supportBody()) { return null; }// www.j a v a2s . c o m if (body != null) { return RequestBody.create(HttpConsts.MEDIA_TYPE_OCTET_STREAM, body); } RequestBody requestBody; if (hasParts()) { final MultipartBuilder multipart = new MultipartBuilder(); for (final BodyPart part : parts()) { if (part.getBody() != null) { multipart.addFormDataPart(part.getName(), part.getFileName(), part.getBody()); } } for (Map.Entry<String, String> entry : form().entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); multipart.addFormDataPart(key, value == null ? "" : value); } requestBody = multipart.type(MultipartBuilder.FORM).build(); } else if (hasForms()) { final FormEncodingBuilder bodyBuilder = new FormEncodingBuilder(); for (Map.Entry<String, String> entry : form().entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); bodyBuilder.add(key, value == null ? "" : value); } requestBody = bodyBuilder.build(); } else { requestBody = null; } return requestBody; }
From source file:com.microsoft.windowsazure.mobileservices.http.ServiceFilterRequestImpl.java
License:Open Source License
public static ServiceFilterRequestImpl post(OkHttpClientFactory factory, String url, byte[] content) { if (content == null) { content = "".getBytes(); }//from w w w . ja v a2 s.co m RequestBody requestBody = RequestBody.create(JSON, content); Request request = getBaseRequestBuilder(url).post(requestBody).build(); return new ServiceFilterRequestImpl(request, factory, content); }
From source file:com.microsoft.windowsazure.mobileservices.http.ServiceFilterRequestImpl.java
License:Open Source License
public static ServiceFilterRequestImpl put(OkHttpClientFactory factory, String url, byte[] content) { if (content == null) { content = "".getBytes(); }/*from www .ja va 2 s .c om*/ RequestBody requestBody = RequestBody.create(JSON, content); Request request = getBaseRequestBuilder(url).put(requestBody).build(); return new ServiceFilterRequestImpl(request, factory, content); }
From source file:com.microsoft.windowsazure.mobileservices.http.ServiceFilterRequestImpl.java
License:Open Source License
public static ServiceFilterRequestImpl patch(OkHttpClientFactory factory, String url, byte[] content) { if (content == null) { content = "".getBytes(); }//w w w.j a va2s. c o m RequestBody requestBody = RequestBody.create(JSON, content); Request request = getBaseRequestBuilder(url).patch(requestBody).build(); return new ServiceFilterRequestImpl(request, factory, content); }
From source file:com.microsoft.windowsazure.mobileservices.http.ServiceFilterRequestImpl.java
License:Open Source License
public static ServiceFilterRequestImpl delete(OkHttpClientFactory factory, String url, byte[] content) { Request.Builder requestBuilder = getBaseRequestBuilder(url); if (content != null) { RequestBody requestBody = RequestBody.create(JSON, content); requestBuilder = requestBuilder.delete(requestBody); } else {/*from www . j a v a 2s .c o m*/ requestBuilder = requestBuilder.delete(); } return new ServiceFilterRequestImpl(requestBuilder.build(), factory, content); }