我正在尝试使用Spring for Android和Robospice来获取图像。我根据GitHub上的各种示例使用createRestTemplate方法设置了我的SpiceService,这是我设置标题。我还设置了Gson,Form和StringHttp消息转换器。
当只获取普通JSON时,此设置工作正常,但是当我尝试获取图像时,标头被设置为某些默认值,这意味着我松开了我的Auth标头并且请求失败。
我还尝试在Request本身内设置一个新的resttemplate。
我在这里缺少什么。
编辑: 用于下载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;
}
}
图像请求看起来像这样,基本上是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;
}
}
}
现在看两个我看到我使用getRestTemplate方法可能会使用我的标题获取RestTemplate。这可能是问题,但有人可以给我一个如何处理的提示吗?
EDIT2: 来自我的SpiceService的createRestTemplate方法
@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;
}
答案 0 :(得分:0)
您也可以并且应该使用RestTemplate
来检索图片:http://blog.springsource.org/2009/03/27/rest-in-spring-3-resttemplate/ - &gt; “检索照片”。