简体   繁体   English

在 Android 中使用 restTemplate.getForObject 反序列化 JSON object 的正确方法

[英]Proper way to use restTemplate.getForObject to deserialize JSON object in Android

I am trying to deserialize a JSON object from Java back to an android class. I am trying to deserialize a JSON object from Java back to an android class. If I use getForObject(url,String.class) the method returns a value, however if I use getForObject(url,Info.class) to deserialize into an object the debugger crashes and doesnt even reach the catch block.如果我使用 getForObject(url,String.class) 该方法返回一个值,但是如果我使用 getForObject(url,Info.class) 反序列化为 object 调试器崩溃并且甚至没有到达 catch 块。 Am I missing something?我错过了什么吗?

 private class HttpRequestTask extends AsyncTask<Void, Void, Info> {
        @Override
        protected Info doInBackground(Void... params) {
            try {

                String url = "http://192.168.1.101:8080/sandbox/android";
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

                // works
                ///String result = restTemplate.getForObject(url, String.class);
                //Log.d("Main",result); // {"id":"1","content":null,"name":"Tommy","number":"6666","email":"abd@zyxo.com"}

                // Info crashes
                Info info2 = restTemplate.getForObject(url, Info.class);
                Log.d("Main","succes");

            } catch (Exception e) {
                Log.e("MainActivity", e.getMessage(), e);
            }

            return null;
        }

Found it!找到了! Caused by: java.lang.NoClassDefFoundError: java.nio.file.Path . Caused by: java.lang.NoClassDefFoundError: java.nio.file.Path Looks like I have to configure my graddle a bit more看起来我必须配置我的 gradle 多一点

from ( NoClassDefFoundError on Android ) You're trying to use java.nio.file.Paths, which doesn't exist in Android as far as I can see.来自( Android 上的 NoClassDefFoundError )您正在尝试使用 java.nio.file.Paths,据 I 可以看到,它在 Android 中不存在。 (Only some parts of the Java 7 API are supported.) (仅支持 Java 7 API 的某些部分。)

Now I'm confused:S现在我很困惑:S

Using使用

implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
implementation 'org.springframework.android:spring-android-core:2.0.0.M3'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0.pr3'

[edit]Found out its a Jackson issue https://github.com/FasterXML/jackson-databind/issues/2466 [/edit] [编辑]发现它是一个 Jackson 问题https://github.com/FasterXML/jackson-databind/issues/2466 [/编辑]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 restTemplate.getForObject上的HttpClientErrorException - HttpClientErrorException on restTemplate.getForObject Spring restTemplate.getForObject()的JSON转换错误 - JSON conversion error from Spring restTemplate.getForObject() restTemplate.getForObject(“URL”,Object [] .class)可以返回NULL吗? - Can restTemplate.getForObject(“URL”, Object[].class) return NULL? 没有获得通过RestTemplate.getForObject传递的标头 - not getting headers passed with RestTemplate.getForObject Spring Boot 微服务 - 将授权标头添加到 restTemplate.getForObject - Spring Boot Microservices - Add authorization header to restTemplate.getForObject 如何对 Mediatypes Rest 端点进行 restTemplate.getForObject 调用 - How to make restTemplate.getForObject call for Mediatypes Rest endpoint 调用restTemplate.getForObject时如何处理未找到数据 - How to handle no data found when calling restTemplate.getForObject 当json属性名称根据请求更改时,请使用restTemplate getForObject - Use restTemplate getForObject when a json property name changes depending on the request 反序列化JSon String int List的正确方法 <Map<String, Object> &gt; - Proper way to Deserialize JSon String int List<Map<String, Object>> Spring RestTemplate GetForObject为JSON响应提供XML特定的异常 - Spring RestTemplate GetForObject giving an XML specific exception for a JSON response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM