Question

I am trying to fetch an image using Spring for Android and Robospice. I have my SpiceService set up with a createRestTemplate method as per the various samples on GitHub and this is were I set my headers. I also set Gson, Form and StringHttp message converters.

This setup works fine when only getting plain JSON, but when I try to fetch an image the headers get set to some default values, meaning I loose my Auth header and the request fails.

I have also tried to set a new resttemplate inside the Request itself.

What am I missing here.

EDIT: The Request used for downloading a json:

public class GetMeRequest extends SpringAndroidSpiceRequest<User> {
public static final String TAG = "GetMeRequest";

public GetMeRequest() {
    super(User.class);
}

@Override
public User loadDataFromNetwork() throws Exception {
    Log.d( TAG, "================loading from network" );
    ResponseObject response = getRestTemplate().getForObject( ApiCalls.USERS_ME_URI, ResponseObject.class );

    Log.d(TAG, response.toString());
    if(!response.data.users.isEmpty() && response.data.users != null){
        return response.data.users.get(0);
    }
    return null;
   }
}

The image request looks like this and is basically a BigBinaryRequest:

public class ImageRequest extends SpringAndroidSpiceRequest<InputStream> {
protected String url;
protected File cacheFile;
protected String auth;

public ImageRequest(String url, File cacheFile, String auth) {
    super(InputStream.class);
    this.url = url;
    this.cacheFile = cacheFile;
    this.auth = auth;
}

@Override
public InputStream loadDataFromNetwork() throws Exception {
    try {
        java.io.InputStream is = new URL(url).openStream();
        // touch
        cacheFile.setLastModified(System.currentTimeMillis());

        FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
        Utils.copyLargeFile(is, fileOutputStream);
        return new FileInputStream(cacheFile);
    } catch (MalformedURLException e) {
        Log.e(getClass().getName(), "Unable to create image URL", e);
        return null;
    } catch (IOException e) {
        Log.e(getClass().getName(), "Unable to download image", e);
        return null;
    }
}
}

Now looking at the two I see that i use the getRestTemplate method wich probably fetches the RestTemplate with my headers. This is probably the problem, but can someone please give me a hint on how to procede?

Edit2: And the createRestTemplate method from my SpiceService

@Override
public RestTemplate createRestTemplate() {
    RestTemplate restTemplate = new RestTemplate() {
        @Override
        protected ClientHttpRequest createRequest( URI url, HttpMethod method ) throws IOException {
            ClientHttpRequest request = super.createRequest( url, method );
            HttpHeaders headers = request.getHeaders();
            headers.setAcceptEncoding( ContentCodingType.GZIP );
            headers.set("Accept", "*/*");
            headers.setUserAgent(P1Application.USER_AGENT);
            headers.setContentType(new MediaType("application","json"));
            headers.set("Authorization", ((P1Application)getApplication()).getAuth());
            return request;
        }
    };


    // set timeout for requests
    ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
    if ( factory instanceof HttpComponentsClientHttpRequestFactory ) {
        HttpComponentsClientHttpRequestFactory advancedFactory = (HttpComponentsClientHttpRequestFactory) factory;
        advancedFactory.setConnectTimeout( TIMEOUT );
        advancedFactory.setReadTimeout( TIMEOUT );
    } else if ( factory instanceof SimpleClientHttpRequestFactory ) {
        SimpleClientHttpRequestFactory advancedFactory = (SimpleClientHttpRequestFactory) factory;
        advancedFactory.setConnectTimeout( TIMEOUT );
        advancedFactory.setReadTimeout( TIMEOUT );
    }

    GsonHttpMessageConverter jsonConverter = new GsonHttpMessageConverter();
    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 );
    return restTemplate;
}
Était-ce utile?

La solution

You can and should use RestTemplate also to retrieve images: http://blog.springsource.org/2009/03/27/rest-in-spring-3-resttemplate/ -> "Retrieving the photos".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top