本文整理汇总了Java中retrofit.converter.ConversionException类的典型用法代码示例。如果您正苦于以下问题:Java ConversionException类的具体用法?Java ConversionException怎么用?Java ConversionException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConversionException类属于retrofit.converter包,在下文中一共展示了ConversionException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
InputStream stream = body.in();
String responseBodyString = MiniIOUtils.toString(stream);
if(responseBodyString.contains("<!DOCTYPE html>"))
return responseBodyString;
JsonParser parser = new JsonParser();
JsonObject element = (JsonObject)parser.parse(responseBodyString);
JsonElement dataObject = element.get("data");
if (!dataObject.isJsonArray() && type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> c = Class.forName(getClassName(componentType));
return Array.newInstance(c, 0);
}
return gson.fromJson(dataObject, type);
} catch (JsonParseException | IOException | ClassNotFoundException e) {
throw new ConversionException(e);
}
}
示例2: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
InputStream stream = body.in();
String responseBodyString = MiniIOUtils.toString(stream);
if (responseBodyString.contains("<!DOCTYPE html>"))
return responseBodyString;
JsonParser parser = new JsonParser();
JsonObject element = (JsonObject) parser.parse(responseBodyString);
JsonElement dataObject = null;
if (element.has("items") && !(element.has("labels") && element.has("desc") && element.has("curl"))) {
dataObject = element.get("items");
} else {
dataObject = element;
}
if (!dataObject.isJsonArray() && type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> c = Class.forName(getClassName(componentType));
return Array.newInstance(c, 0);
}
return gson.fromJson(dataObject, type);
} catch (JsonParseException | IOException | ClassNotFoundException e) {
throw new ConversionException(e);
}
}
示例3: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
boolean willCloseStream = false; // try to close the stream, if there is no exception thrown
// using tolerant JsonReader
try {
JsonReader jsonReader = new JsonReader(new InputStreamReader(body.in()));
jsonReader.setLenient(true);
Object o = mGson.fromJson(jsonReader, type);
willCloseStream = true;
return o;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (willCloseStream) {
closeStream(body);
}
}
return super.fromBody(body, type);
}
示例4: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
// Check if the type contains a parametrized list
if (ParameterizedType.class.isAssignableFrom(type.getClass())) {
// Grab the actual type parameter from the parametrized list and delegate to LoganSquare
ParameterizedType parameterized = (ParameterizedType) type;
return LoganSquare.parseList(body.in(), (Class) parameterized.getActualTypeArguments()[0]);
} else {
// Single elements get parsed immediately
return LoganSquare.parse(body.in(), (Class) type);
}
} catch (Exception e) {
throw new ConversionException(e);
}
}
示例5: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
String text = null;
try {
typedInput.in();
BufferedReader reader = new BufferedReader(new InputStreamReader(typedInput.in()));
StringBuilder out = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(newLine);
}
text = out.toString();
} catch (IOException ignored) {/*NOP*/ }
return text;
}
示例6: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput typedinput, Type type)
throws ConversionException {
if (typedinput.length() == 0L)
{
return null;
}
try
{
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
if (type.getClass().equals(Void.TYPE)) {
return null;
}
return objectMapper.readValue(typedinput.in(), javaType);
} catch (IOException e) {
throw new ConversionException(e);
}
}
示例7: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
// Check if the type contains a parametrized list
if (ParameterizedType.class.isAssignableFrom(type.getClass())) {
// Grab the actual type parameter from the parametrized list and delegate to LoganSquare
ParameterizedType parameterized = (ParameterizedType) type;
return LoganSquare.parseList(body.in(), (Class) parameterized.getActualTypeArguments()[0]);
} else {
// Single elements get parsed immediately
return LoganSquare.parse(body.in(), (Class) type);
}
} catch (Exception e) {
throw new ConversionException(e);
}
}
示例8: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
String charset = "UTF-8";
if (body.mimeType() != null) {
charset = MimeUtil.parseCharset(body.mimeType());
}
InputStreamReader isr = null;
try {
isr = new InputStreamReader(body.in(), charset);
return gson.fromJson(isr, type);
} catch (IOException | JsonParseException e) {
throw new ConversionException(e);
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException ex) {
// Ignored
}
}
}
}
示例9: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
// Check if the type contains a parametrized list
if (ParameterizedType.class.isAssignableFrom(type.getClass())) {
// Grab the actual type parameter from the parametrized list and delegate to LoganSquare
ParameterizedType parameterized = (ParameterizedType) type;
return LoganSquare.parseList(body.in(), (Class) parameterized.getActualTypeArguments()[0]);
} else {
// Single elements get parsed immediately
return LoganSquare.parse(body.in(), (Class) type);
}
} catch (Exception e) {
throw new ConversionException(e);
}
}
示例10: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
try {
InputStream in = typedInput.in();
try {
return objectMapper.readValue(in, javaType);
} finally {
if (in != null) {
in.close();
}
}
} catch (IOException e) {
throw new ConversionException(e);
}
}
示例11: deserializes_product_from_feed
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Test
public void deserializes_product_from_feed() throws ConversionException {
String feed = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:vidal=\"http://api.vidal.net/-/spec/vidal-api/1.0/\">" +
" <entry>" +
" <vidal:id>42</vidal:id>" +
" <title>Batman</title>" +
" </entry>" +
"</feed>";
Product product = deserializer.deserializeOne(feed);
assertThat(product)
.hasId("42")
.hasName("Batman");
}
示例12: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
if (type != Token.class) {
return mOriginalConverter.fromBody(body, type);
}
// We don't check for null refresh token, it's optional - it's retrieved on initial auth but not on a token refresh
TokenResponse response = (TokenResponse) mOriginalConverter.fromBody(body,
TokenResponse.class);
if (response == null || response.getAccessToken() == null) {
throw new ConversionException("Empty/missing token response: " + response);
}
return new Token.Builder().accessToken(response.getAccessToken())
.refreshToken(response.getRefreshToken())
.expiryTimeMillis(DateTime.now(DateTimeZone.UTC)
.plusSeconds(response.getExpiresIn())
.getMillis())
.build();
}
示例13: fromBody
import retrofit.converter.ConversionException; //导入依赖的package包/类
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
Scanner s = new Scanner(body.in()).useDelimiter("\\A");
String bodyText = s.hasNext() ? s.next() : "";
boolean isSuccessResponse = bodyText.startsWith("202 Accepted");
MarkAllRead markAllRead = new MarkAllRead();
if (!isSuccessResponse) {
markAllRead.setErrors(bodyText);
}
return markAllRead;
} catch (IOException e) {
throw new ConversionException(e);
}
}
示例14: convertComments
import retrofit.converter.ConversionException; //导入依赖的package包/类
private List<Comment> convertComments(String filename) throws IOException, ConversionException {
final TypedInput body = mock(TypedInput.class);
when(body.in()).thenReturn(TestUtils.loadFileFromStream(filename));
final CommentsConverter commentsConverter = new CommentsConverter(mGson,
mGsonConverter,
mResources,
mUserStorage);
final List<Comment> comments = (List<Comment>) commentsConverter.fromBody(body,
new ParameterizedType() {
@Override public Type[] getActualTypeArguments() {
return new Type[]{Comment.class};
}
@Override public Type getOwnerType() {
return null;
}
@Override public Type getRawType() {
return List.class;
}
});
return comments;
}
示例15: convertPostResponse
import retrofit.converter.ConversionException; //导入依赖的package包/类
private Post convertPostResponse(String filename) throws IOException, ConversionException {
final TypedInput body = mock(TypedInput.class);
when(body.in()).thenReturn(TestUtils.loadFileFromStream(filename));
final PostConverter postConverter = new PostConverter(mGsonConverter,
mResources,
mUserStorage,
mHtmlDecoder);
final List<Post> posts = (List<Post>) postConverter.fromBody(body, new ParameterizedType() {
@Override public Type[] getActualTypeArguments() {
return new Type[]{Post.class};
}
@Override public Type getOwnerType() {
return null;
}
@Override public Type getRawType() {
return List.class;
}
});
assertThat(posts.size(), equalTo(1));
return posts.get(0);
}