本文整理匯總了Java中com.squareup.okhttp.RequestBody.writeTo方法的典型用法代碼示例。如果您正苦於以下問題:Java RequestBody.writeTo方法的具體用法?Java RequestBody.writeTo怎麽用?Java RequestBody.writeTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.okhttp.RequestBody
的用法示例。
在下文中一共展示了RequestBody.writeTo方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: forceContentLength
import com.squareup.okhttp.RequestBody; //導入方法依賴的package包/類
/**
* https://github.com/square/okhttp/issues/350
*/
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
final Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return new RequestBody() {
@Override
public MediaType contentType() {
return requestBody.contentType();
}
@Override
public long contentLength() {
return buffer.size();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(buffer.snapshot());
}
};
}
示例2: gzip
import com.squareup.okhttp.RequestBody; //導入方法依賴的package包/類
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override public MediaType contentType() {
return body.contentType();
}
@Override public long contentLength() {
return -1; // 無法知道壓縮後的數據大小
}
@Override public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
示例3: gzip
import com.squareup.okhttp.RequestBody; //導入方法依賴的package包/類
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
return -1; // We don't know the compressed length in advance!
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
示例4: extractBodyParams
import com.squareup.okhttp.RequestBody; //導入方法依賴的package包/類
static Map<String, String> extractBodyParams(RequestBody body) {
// extract form-encoded HTTP body params
final Map<String, String> bodyParams = new HashMap<>();
if (body != null && body.contentType().equals(FORM_CONTENT_TYPE)) {
final Buffer buffer = new Buffer();
try {
body.writeTo(buffer);
while (!buffer.exhausted()) {
long keyEnd = buffer.indexOf((byte) '=');
if (keyEnd == -1)
throw new IllegalStateException("Key with no value: " + buffer.readUtf8());
String key = buffer.readUtf8(keyEnd);
buffer.skip(1); // Equals.
long valueEnd = buffer.indexOf((byte) '&');
String value = valueEnd == -1 ? buffer.readUtf8() : buffer.readUtf8(valueEnd);
if (valueEnd != -1) buffer.skip(1); // Ampersand.
bodyParams.put(key, value);
}
} catch (IOException e) {
}
}
return bodyParams;
}
示例5: extractBodyParams
import com.squareup.okhttp.RequestBody; //導入方法依賴的package包/類
static Map<String, String> extractBodyParams(RequestBody body) {
// extract form-encoded HTTP body params
final Map<String, String> bodyParams = new HashMap<>();
if (body != null && body.contentType().equals(FORM_CONTENT_TYPE)) {
final Buffer buffer = new Buffer();
try {
body.writeTo(buffer);
while (!buffer.exhausted()) {
long keyEnd = buffer.indexOf((byte) '=');
if (keyEnd == -1)
throw new IllegalStateException("Key with no value: " + buffer.readUtf8());
String key = buffer.readUtf8(keyEnd);
buffer.skip(1); // Equals.
long valueEnd = buffer.indexOf((byte) '&');
String value = valueEnd == -1 ? buffer.readUtf8() : buffer.readUtf8(valueEnd);
if (valueEnd != -1) buffer.skip(1); // Ampersand.
bodyParams.put(PercentEncoder.decode(key), value);
}
} catch (IOException e) {
}
}
return bodyParams;
}