2

我用 Robospice 开发了一个 Android 应用程序。从我的移动应用程序到我的远程服务器的交换基于请求和答案的 Json(Http GET 和 POST)。

所以我创建了一个这样的 JsonSpiceService :

// @see https://github.com/octo-online/robospice/wiki/Starter-Guide#wiki-create-a-spicerequest-and-a-listener
public class JsonSpiceService extends SpringAndroidSpiceService {

    @Override
    public CacheManager createCacheManager(Application application)
            throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        JacksonObjectPersisterFactory jacksonObjectPersisterFactory =
                new JacksonObjectPersisterFactory(application);
        cacheManager.addPersister(jacksonObjectPersisterFactory);
        return cacheManager;
    }

    @Override
    public RestTemplate createRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        // find more complete examples in RoboSpice Motivation app
        // to enable Gzip compression and setting request timeouts.

        // web services support json responses
        MappingJacksonHttpMessageConverter jsonConverter =
                new MappingJacksonHttpMessageConverter();
        FormHttpMessageConverter formHttpMessageConverter =
                new FormHttpMessageConverter();
        StringHttpMessageConverter stringHttpMessageConverter =
                new StringHttpMessageConverter();
        final List<HttpMessageConverter<?>> listHttpMessageConverters =
                restTemplate.getMessageConverters();

        listHttpMessageConverters.add(jsonConverter);
        listHttpMessageConverters.add(formHttpMessageConverter);
        listHttpMessageConverters.add(stringHttpMessageConverter);
        restTemplate.setMessageConverters(listHttpMessageConverters);

        // @see http://sapandiwakar.in/eofexception-with-spring-rest-template-android/
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); 

        return restTemplate;
    }
}

我的 GET 请求:

public class GetDevicesRequest extends SpringAndroidSpiceRequest<DeviceList> {

    public GetDevicesRequest() {
        super(DeviceList.class);
    }

    @Override
    public DeviceList loadDataFromNetwork() throws Exception {
        URL url = new URL("bla bla bla"); 
        return getRestTemplate().getForObject(url.toURI(), DeviceList.class);
    }

此请求与标准服务 JacksonSpringAndroidSpiceService 一起发送:

protected SpiceManager spiceManager = new SpiceManager(
            JacksonSpringAndroidSpiceService.class);

spiceManager.execute(request, new GetDevicesRequestListener());

我的 POST 请求:

public class SetKelvinServiceRequest extends SpringAndroidSpiceRequest<RequestGenericResult> {

    @Override
    public RequestGenericResult loadDataFromNetwork() throws Exception {

        KelvinService service = new KelvinService(this.service, this.value);

        URL url = new URL("bla bla bla"); 

        return getRestTemplate().postForObject(url.toURI(), service,
                RequestGenericResult.class);
    }

此请求与我的扩展服务 JsonSpiceService 一起发送:

protected SpiceManager spiceJsonManager = new SpiceManager(
        JsonSpiceService.class);

spiceJsonManager.execute(request, new SetKelvinServiceRequestListener());

当我想发送带有 Json 数据(POST)的请求时,它可以工作。但是当我发送 GET 请求时它不起作用;收到的结果始终为空(我的 Json 反序列化)。对于我的 GET 请求,我必须使用标准服务:JacksonSpringAndroidSpiceService

但我不知道为什么我的 JsonSpiceService 不适用于 GET 请求......

感谢你们 !

4

0 回答 0