我正在尝试使用 Spring for Android 和 Robospice 获取图像。根据 GitHub 上的各种示例,我使用 createRestTemplate 方法设置了我的 SpiceService,这是我设置我的标题。我还设置了 Gson、Form 和 StringHttp 消息转换器。
此设置仅在获取纯 JSON 时工作正常,但是当我尝试获取图像时,标头会设置为一些默认值,这意味着我丢失了 Auth 标头并且请求失败。
我还尝试在请求本身内设置一个新的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;
}