1

i am developing android application with maven and spring resttemplate.

This is my pom.xml

<properties>
    <platform.version> 2.3.3</platform.version>
    <spring.version>1.0.0.RELEASE</spring.version>
    <simple-version>2.4.1</simple-version>
</properties>

<dependencies>

    <!-- Spring Android Core -->
    <dependency>
        <groupId>org.springframework.android</groupId>
        <artifactId>spring-android-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Spring Rest Android -->
    <dependency>
        <groupId>org.springframework.android</groupId>
        <artifactId>spring-android-rest-template</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Using Jackson for JSON marshaling -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.1</version>
    </dependency>

    <!-- Using Simple for XML marshaling -->
    <dependency>
        <groupId>org.simpleframework</groupId>
        <artifactId>simple-xml</artifactId>
        <version>${simple-version}</version>
        <exclusions>
            <exclusion>
                <artifactId>stax</artifactId>
                <groupId>stax</groupId>
            </exclusion>
            <exclusion>
                <artifactId>stax-api</artifactId>
                <groupId>stax</groupId>
            </exclusion>
        </exclusions>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                <resourceDirectory>${project.basedir}/res</resourceDirectory>
                <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                <sdk>
                    <platform>10</platform>
                </sdk>
                <emulator>
                    <avd>10</avd>
                </emulator>
                <deleteConflictingFiles>true</deleteConflictingFiles>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
            </configuration>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

In main Activity i have this code into button:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnLogin = (Button) findViewById(R.id.btnLogin);

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            String email = "user@xxxx.com";
            String passwd = "123456";

            restTemplate = new RestTemplate();

            try {
                Users user = restTemplate.getForObject(URL + "/{email}/{password}",
                                Users.class, email, passwd);

                if (user != null) {
                    System.out.println("Mi nombre es: " + user);
                }

            } catch (ResourceAccessException ex) {
                System.out.println("OFF");
            } catch (RestClientException ex) {
                System.out.println("Error");
            }
        }
    });

When i run my aaplication into device this is the error that show in logCat:

java.lang-IllegalArgumentException: 'messageConverters' must not be empty

Thansk!!!!

Josymar De Leon
  • 147
  • 4
  • 11

3 Answers3

5

Set the message converter

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
Users user = restTemplate.getForObject(URL + "/{email}/{password}",
                            Users.class, email, passwd);

Please see more here at section 2.7.3 Retrieving JSON data via HTTP GET in Spring for Android Reference Manual.

And a good example here.

Misagh Aghakhani
  • 1,023
  • 2
  • 13
  • 29
2

You need to set the message converter, so that the response could be parsed into an User instance. Depending on the response type from the server you are using, you should provide different converters, e.g. if you need to parse a JSON you could use:

restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

In case of using some of the predefined converters you might need to include some additional libraries into your project (in this example you'd need a Jackson lib).

toommm
  • 42
  • 2
1

I found the solution using try/catch and inside method:

public Users obtainUser() {
    try {

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

        RestTemplate restTemplate = new RestTemplate();

        restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

        ResponseEntity<Users> responseEntity = restTemplate.exchange(URL, HttpMethod.GET, requestEntity, Users.class);
        Users users = responseEntity.getBody();

        return users;


    } catch (Exception e) {
        System.out.println("Error: " + e);
        return null;
    }
}

using android class AsyncTask too

Josymar De Leon
  • 147
  • 4
  • 11