带有Robospice的Android:使用Json数据获取GET请求

时间:2014-03-04 07:51:27

标签: android json http robospice

我使用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请求时它不起作用;收到的结果总是为null(我的Json反序列化)。对于我的GET请求,我必须使用标准服务:JacksonSpringAndroidSpiceService

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

谢谢你们!

0 个答案:

没有答案