本文整理了Java中okhttp3.RequestBody.create
方法的一些代码示例,展示了RequestBody.create
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestBody.create
方法的具体详情如下:
包路径:okhttp3.RequestBody
类名称:RequestBody
方法名:create
[英]Returns a new request body that transmits the content of file.
[中]返回传输文件内容的新请求正文。
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
return RequestBody.create(MEDIA_TYPE, String.valueOf(value));
}
}
代码示例来源:origin: square/okhttp
/** Returns a new request body that transmits {@code content}. */
public static RequestBody create(final @Nullable MediaType contentType, final byte[] content) {
return create(contentType, content, 0, content.length);
}
代码示例来源:origin: square/okhttp
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
代码示例来源:origin: square/okhttp
public void run() throws Exception {
File file = new File("README.md");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
byte[] bytes = value.toByteArray();
return RequestBody.create(MEDIA_TYPE, bytes);
}
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
byte[] bytes = adapter.writeValueAsBytes(value);
return RequestBody.create(MEDIA_TYPE, bytes);
}
}
代码示例来源:origin: square/okhttp
private RequestBody getRequestBody() {
if (data == null) {
return null;
}
String bodyData = data;
String mimeType = "application/x-www-form-urlencoded";
if (headers != null) {
for (String header : headers) {
String[] parts = header.split(":", -1);
if ("Content-Type".equalsIgnoreCase(parts[0])) {
mimeType = parts[1].trim();
headers.remove(header);
break;
}
}
}
return RequestBody.create(MediaType.parse(mimeType), bodyData);
}
代码示例来源:origin: square/okhttp
public static Part createFormData(String name, String value) {
return createFormData(name, null, RequestBody.create(null, value));
}
代码示例来源:origin: square/okhttp
public void run() throws Exception {
Map<String, String> requestBody = new LinkedHashMap<>();
requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/");
RequestBody jsonRequestBody = RequestBody.create(
MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody));
Request request = new Request.Builder()
.url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY)
.post(jsonRequestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
代码示例来源:origin: square/okhttp
public void run() throws Exception {
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
代码示例来源:origin: square/okhttp
/**
* Returns a new request body that transmits {@code content}. If {@code contentType} is non-null
* and lacks a charset, this will use UTF-8.
*/
public static RequestBody create(@Nullable MediaType contentType, String content) {
Charset charset = UTF_8;
if (contentType != null) {
charset = contentType.charset();
if (charset == null) {
charset = UTF_8;
contentType = MediaType.parse(contentType + "; charset=utf-8");
}
}
byte[] bytes = content.getBytes(charset);
return create(contentType, bytes);
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(final T value) throws IOException {
Buffer buffer = new Buffer();
try {
Marshaller marshaller = context.createMarshaller();
XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter(
buffer.outputStream(), JaxbConverterFactory.XML.charset().name());
marshaller.marshal(value, xmlWriter);
} catch (JAXBException | XMLStreamException e) {
throw new RuntimeException(e);
}
return RequestBody.create(JaxbConverterFactory.XML, buffer.readByteString());
}
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
adapter.encode(buffer, value);
return RequestBody.create(MEDIA_TYPE, buffer.snapshot());
}
}
代码示例来源:origin: square/okhttp
private Request buildRequest(String hostname, int type) {
Request.Builder requestBuilder = new Request.Builder().header("Accept", DNS_MESSAGE.toString());
ByteString query = DnsRecordCodec.encodeQuery(hostname, type);
if (post) {
requestBuilder = requestBuilder.url(url).post(RequestBody.create(DNS_MESSAGE, query));
} else {
String encoded = query.base64Url().replace("=", "");
HttpUrl requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build();
requestBuilder = requestBuilder.url(requestUrl);
}
return requestBuilder.build();
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
try {
OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET);
serializer.write(value, osw);
osw.flush();
} catch (RuntimeException | IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}
代码示例来源:origin: square/okhttp
public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
.build();
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.url("https://api.imgur.com/3/image")
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
JsonWriter writer = JsonWriter.of(buffer);
adapter.toJson(writer, value);
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}
代码示例来源:origin: spring-projects/spring-framework
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method)
throws MalformedURLException {
okhttp3.MediaType contentType = getContentType(headers);
RequestBody body = (content.length > 0 ||
okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ?
RequestBody.create(contentType, content) : null);
Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body);
headers.forEach((headerName, headerValues) -> {
for (String headerValue : headerValues) {
builder.addHeader(headerName, headerValue);
}
});
return builder.build();
}
代码示例来源:origin: square/retrofit
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}
代码示例来源:origin: alibaba/fastjson
public RequestBody convert(T value) throws IOException {
try {
byte[] content = JSON.toJSONBytes(fastJsonConfig.getCharset()
, value
, fastJsonConfig.getSerializeConfig()
, fastJsonConfig.getSerializeFilters()
, fastJsonConfig.getDateFormat()
, JSON.DEFAULT_GENERATE_FEATURE
, fastJsonConfig.getSerializerFeatures()
);
return RequestBody.create(MEDIA_TYPE, content);
} catch (Exception e) {
throw new IOException("Could not write JSON: " + e.getMessage(), e);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!