简体   繁体   中英

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. 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. 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 . Looks like I have to configure my graddle a bit more

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. (Only some parts of the Java 7 API are supported.)

Now I'm confused: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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM