本文整理匯總了Java中retrofit.mime.TypedOutput類的典型用法代碼示例。如果您正苦於以下問題:Java TypedOutput類的具體用法?Java TypedOutput怎麽用?Java TypedOutput使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TypedOutput類屬於retrofit.mime包,在下文中一共展示了TypedOutput類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Override
public TypedOutput toBody(Object object) {
String jsonStr = null;
try {
JSONObject jsonObj = toJson(object);
if (jsonObj != null) {
jsonStr = jsonObj.toString();
}
} catch (JSONException e) {
// TODO: do something
e.printStackTrace();
}
return new TypedJsonString(jsonStr);
}
示例2: createRequestBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
private static RequestBody createRequestBody(final TypedOutput body) {
if (body == null) {
return null;
}
final MediaType mediaType = MediaType.parse(body.mimeType());
return new RequestBody() {
@Override public MediaType contentType() {
return mediaType;
}
@Override public void writeTo(BufferedSink sink) throws IOException {
body.writeTo(sink.outputStream());
}
@Override public long contentLength() {
return body.length();
}
};
}
示例3: toBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@SuppressWarnings("unchecked") @Override public TypedOutput toBody(Object object) {
try {
// Check if the type contains a parametrized list
if (List.class.isAssignableFrom(object.getClass())) {
// Convert the input to a list first, access the first element and serialize the list
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new TypedString("[]");
} else {
Object firstElement = list.get(0);
return new TypedString(LoganSquare.serialize(list, (Class<Object>) firstElement.getClass()));
}
} else {
// Serialize single elements immediately
return new TypedString(LoganSquare.serialize(object));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例4: testPatchResource
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Test
public void testPatchResource() throws Exception {
Map<String,Object> fooBar = new LinkedHashMap<>();
fooBar.put("foo", 1);
fooBar.put("bar", 2);
Map <String, Map<String,Object>> properties = new LinkedHashMap<>();
properties.put("custom_properties", fooBar);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create();
TypedOutput body = new GsonConverter(gson).toBody(properties);
Resource resource = client.patchResource(new ResourcesArgs.Builder()
.setPath("/0-test")
.setBody(body)
.build());
assertTrue("dir".equals(resource.getType()));
assertEquals(resource.getPath(), new ResourcePath("disk", "/0-test"));
logger.info("self: " + resource);
}
示例5: toBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@SuppressWarnings("unchecked") @Override public TypedOutput toBody(Object object) {
try {
// Check if the type contains a parametrized list
if (List.class.isAssignableFrom(object.getClass())) {
// Convert the input to a list first, access the first element and serialize the list
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new TypedString("[]");
} else {
Object firstElement = list.get(0);
return new TypedString(
LoganSquare.serialize(list, (Class<Object>) firstElement.getClass()));
}
} else {
// Serialize single elements immediately
return new TypedString(LoganSquare.serialize(object));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例6: GenericHttpRequest
import retrofit.mime.TypedOutput; //導入依賴的package包/類
GenericHttpRequest(Request request) {
super();
method = request.getMethod();
setURI(URI.create(request.getUrl()));
// Add all headers.
for (Header header : request.getHeaders()) {
addHeader(new BasicHeader(header.getName(), header.getValue()));
}
// Add the content body, if any.
TypedOutput body = request.getBody();
if (body != null) {
setEntity(new TypedOutputEntity(body));
}
}
示例7: Request
import retrofit.mime.TypedOutput; //導入依賴的package包/類
public Request(String method, String url, List<Header> headers, TypedOutput body) {
if (method == null) {
throw new NullPointerException("Method must not be null.");
}
if (url == null) {
throw new NullPointerException("URL must not be null.");
}
this.method = method;
this.url = url;
if (headers == null) {
this.headers = Collections.emptyList();
} else {
this.headers = Collections.unmodifiableList(new ArrayList<Header>(headers));
}
this.body = body;
}
示例8: toBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@SuppressWarnings("unchecked") @Override public TypedOutput toBody(Object object) {
try {
// Check if the type contains a parametrized list
if (List.class.isAssignableFrom(object.getClass())) {
// Convert the input to a list first, access the first element and serialize the list
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new TypedString("[]");
} else {
Object firstElement = list.get(0);
return new TypedString(LoganSquare.serialize(list, (Class<Object>) firstElement.getClass()));
}
} else {
// Serialize single elements immediately
return new TypedString(LoganSquare.serialize(object));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例9: toBody
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Override
public TypedOutput toBody(Object object) {
try {
// Check if the type contains a parametrized list
if (List.class.isAssignableFrom(object.getClass())) {
// Convert the input to a list first, access the first element and serialize the list
List<Object> list = (List<Object>) object;
if (list.isEmpty()) {
return new TypedString("[]");
}
else {
Object firstElement = list.get(0);
return new TypedString(LoganSquare.serialize(list, (Class<Object>) firstElement.getClass()));
}
}
else {
// Serialize single elements immediately
return new TypedString(LoganSquare.serialize(object));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例10: testPOSTDataEcho
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Test
public void testPOSTDataEcho() throws Exception {
// Given
final String postBodyString = "hello";
HttpRequestClient httpRequestClient = new HttpRequestClient();
TypedOutput postBody = new TypedString(postBodyString);
Request request = new Request("POST", HTTP_BIN_ROOT + "/post", null, postBody);
// When
final Response response = httpRequestClient.execute(request);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> jsonObj = objectMapper.readValue(response.getBody().in(), Map.class);
// Then
assertNotNull(response);
assertThat(response.getStatus(), is(200));
assertThat(jsonObj.get("data").toString(), is(postBodyString));
}
示例11: getRequestInfo
import retrofit.mime.TypedOutput; //導入依賴的package包/類
private static Profiler.RequestInformation getRequestInfo(String serverUrl,
RestMethodInfo methodDetails, Request request)
{
long contentLength = 0;
String contentType = null;
TypedOutput body = request.getBody();
if (body != null)
{
contentLength = body.length();
contentType = body.mimeType();
}
return new Profiler.RequestInformation(methodDetails.requestMethod, serverUrl,
methodDetails.requestUrl, contentLength, contentType);
}
示例12: GenericHttpRequest
import retrofit.mime.TypedOutput; //導入依賴的package包/類
GenericHttpRequest(Request request)
{
super();
method = request.getMethod();
setURI(URI.create(request.getUrl()));
// Add all headers.
for (Header header : request.getHeaders())
{
addHeader(new BasicHeader(header.getName(), header.getValue()));
}
// Add the content body, if any.
TypedOutput body = request.getBody();
if (body != null)
{
setEntity(new TypedOutputEntity(body));
}
}
示例13: Request
import retrofit.mime.TypedOutput; //導入依賴的package包/類
public Request(String method, String url, List<Header> headers, TypedOutput body)
{
if (method == null)
{
throw new NullPointerException("Method must not be null.");
}
if (url == null)
{
throw new NullPointerException("URL must not be null.");
}
this.method = method;
this.url = url;
if (headers == null)
{
this.headers = Collections.emptyList();
}
else
{
this.headers = Collections.unmodifiableList(new ArrayList<Header>(headers));
}
this.body = body;
}
示例14: bodyTypedBytes
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Test
public void bodyTypedBytes()
{
class Example
{
@PUT
@Path("/")
Response a(TypedOutput o)
{
return null;
}
}
Method method = TestingUtils.getMethod(Example.class, "a");
RestMethodInfo methodInfo = new RestMethodInfo(method);
methodInfo.init();
assertThat(methodInfo.requestParamNames).hasSize(1).containsExactly(new String[]{null});
assertThat(methodInfo.requestParamUsage).hasSize(1).containsExactly(BODY);
assertThat(methodInfo.requestType).isEqualTo(SIMPLE);
}
示例15: multipart
import retrofit.mime.TypedOutput; //導入依賴的package包/類
@Test
public void multipart() throws Exception
{
Map<String, TypedOutput> bodyParams = new LinkedHashMap<String, TypedOutput>();
bodyParams.put("foo", new TypedString("bar"));
bodyParams.put("ping", new TypedString("pong"));
TypedOutput body = TestingUtils.createMultipart(bodyParams);
Request request = new Request("POST", HOST + "/that/", null, body);
DummyHttpUrlConnection connection = (DummyHttpUrlConnection) client.openConnection(request);
client.prepareRequest(connection, request);
byte[] output = connection.getOutputStream().toByteArray();
assertThat(connection.getRequestMethod()).isEqualTo("POST");
assertThat(connection.getURL().toString()).isEqualTo(HOST + "/that/");
assertThat(connection.getRequestProperties()).hasSize(2);
assertThat(connection.getRequestProperty("Content-Type")).startsWith("multipart/form-data;");
assertThat(connection.getRequestProperty("Content-Length")).isEqualTo(String.valueOf(output.length));
assertThat(output.length).isGreaterThan(0);
}