1

我正在使用 maven 和 spring resttemplate 开发 android 应用程序。

这是我的 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>

在主活动中,我将此代码放入按钮中:

@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");
            }
        }
    });

当我在设备中运行我的应用程序时,这是 logCat 中显示的错误:

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

谢谢!!!!

4

3 回答 3

5

设置消息转换器

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

2.7.3 Retrieving JSON data via HTTP GETSpring for Android Reference Manual中查看更多信息。

是一个很好的例子。

于 2012-10-10T12:57:03.017 回答
2

您需要设置消息转换器,以便可以将响应解析为用户实例。根据您使用的服务器的响应类型,您应该提供不同的转换器,例如,如果您需要解析 JSON,您可以使用:

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

如果使用一些预定义的转换器,您可能需要在项目中包含一些额外的库(在本例中,您需要一个 Jackson 库)。

于 2012-09-26T12:19:18.847 回答
1

我使用 try/catch 和 inside 方法找到了解决方案:

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;
    }
}

也使用 android 类 AsyncTask

于 2012-10-30T19:05:09.473 回答