当我使用 Spring 模块实现 Robospice 时,GCM 服务停止工作
GCM service stops working when I implement Robospice with Spring module
我已经按照 google 开发者页面 (Gcm implementation) 上的说明实施了标准 GCM 客户端。推送消息在那里工作得很好没问题。但是后来我在应用程序中实现了 Robospice REST lib,GCM 停止了工作。我确信它因为 Robospice 而不起作用,因为一旦我删除 SpiceManager GCM 的代码就会开始工作。
API 通过 Robospice Spring 模块调用工作正常。这是我对 Robospice 服务的声明:
enter<receiver
android:name="com.clover.spika.enterprise.chat.services.gcm.GcmBroadcastRe ceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.clover.spika.enterprise.chat.gcm" />
</intent-filter>
</receiver>
<service android:name="com.clover.spika.enterprise.chat.services.gcm.GcmIntentService" />
<service
android:name="com.clover.spika.enterprise.chat.services.robospice.Jackson2 SpiceService"
android:exported="false" />
<service android:name="com.clover.spika.enterprise.chat.services.custom.PoolingServ ice" /> code here
这是我的定制香料服务:
package com.clover.spika.enterprise.chat.services.robospice;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.ContentCodingType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import android.app.Application;
import android.app.Notification;
import com.octo.android.robospice.SpringAndroidSpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.springandroid.json.jackson.JacksonO bjectPersisterFactory;
public class Jackson2SpiceService extends SpringAndroidSpiceService {
// private static final int WEBSERVICES_TIMEOUT = 10000;
@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.ALL);
return request;
}
};
// bug on http connection for Android < 2.2
// http://android-developers.blogspot.fr/2011/09/androids-http-clients.html
// but still a problem for upload with Spring-android on android 4.1
// System.setProperty("http.keepAlive", "false");
// // set timeout for requests
// ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
// if (factory instanceof HttpComponentsClientHttpRequestFactory) {
// HttpComponentsClientHttpRequestFactory advancedFactory =
// (HttpComponentsClientHttpRequestFactory) factory;
// advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
// advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
// } else if (factory instanceof SimpleClientHttpRequestFactory) {
// SimpleClientHttpRequestFactory advancedFactory =
// (SimpleClientHttpRequestFactory) factory;
// advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
// advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
// }
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
// StringHttpMessageConverter stringHttpMessageConverter = new
// StringHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
// stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();
listHttpMessageConverters.add(jsonConverter);
listHttpMessageConverters.add(formHttpMessageConverter);
// listHttpMessageConverters.add(stringHttpMessageConverter);
restTemplate.setMessageConverters(listHttpMessageConverters);
return restTemplate;
}
@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory(application);
cacheManager.addPersister(jacksonObjectPersisterFactory);
return cacheManager;
}
@Override
public Notification createDefaultNotification() {
return null;
}
}
有谁知道可能是什么问题?如果需要,我可以 post 附加代码。
我已经解决了这个问题,这不是 robospice 或 gcm 故障,而是我们服务器处理中的问题。
问题是我们从 User-Agent header 读取了一些我不知道的值,我没有将它设置到 OkHttp 客户端。
我已经按照 google 开发者页面 (Gcm implementation) 上的说明实施了标准 GCM 客户端。推送消息在那里工作得很好没问题。但是后来我在应用程序中实现了 Robospice REST lib,GCM 停止了工作。我确信它因为 Robospice 而不起作用,因为一旦我删除 SpiceManager GCM 的代码就会开始工作。
API 通过 Robospice Spring 模块调用工作正常。这是我对 Robospice 服务的声明:
enter<receiver
android:name="com.clover.spika.enterprise.chat.services.gcm.GcmBroadcastRe ceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.clover.spika.enterprise.chat.gcm" />
</intent-filter>
</receiver>
<service android:name="com.clover.spika.enterprise.chat.services.gcm.GcmIntentService" />
<service
android:name="com.clover.spika.enterprise.chat.services.robospice.Jackson2 SpiceService"
android:exported="false" />
<service android:name="com.clover.spika.enterprise.chat.services.custom.PoolingServ ice" /> code here
这是我的定制香料服务:
package com.clover.spika.enterprise.chat.services.robospice;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.ContentCodingType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import android.app.Application;
import android.app.Notification;
import com.octo.android.robospice.SpringAndroidSpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.springandroid.json.jackson.JacksonO bjectPersisterFactory;
public class Jackson2SpiceService extends SpringAndroidSpiceService {
// private static final int WEBSERVICES_TIMEOUT = 10000;
@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.ALL);
return request;
}
};
// bug on http connection for Android < 2.2
// http://android-developers.blogspot.fr/2011/09/androids-http-clients.html
// but still a problem for upload with Spring-android on android 4.1
// System.setProperty("http.keepAlive", "false");
// // set timeout for requests
// ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
// if (factory instanceof HttpComponentsClientHttpRequestFactory) {
// HttpComponentsClientHttpRequestFactory advancedFactory =
// (HttpComponentsClientHttpRequestFactory) factory;
// advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
// advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
// } else if (factory instanceof SimpleClientHttpRequestFactory) {
// SimpleClientHttpRequestFactory advancedFactory =
// (SimpleClientHttpRequestFactory) factory;
// advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
// advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
// }
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
// StringHttpMessageConverter stringHttpMessageConverter = new
// StringHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
// stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();
listHttpMessageConverters.add(jsonConverter);
listHttpMessageConverters.add(formHttpMessageConverter);
// listHttpMessageConverters.add(stringHttpMessageConverter);
restTemplate.setMessageConverters(listHttpMessageConverters);
return restTemplate;
}
@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory(application);
cacheManager.addPersister(jacksonObjectPersisterFactory);
return cacheManager;
}
@Override
public Notification createDefaultNotification() {
return null;
}
}
有谁知道可能是什么问题?如果需要,我可以 post 附加代码。
我已经解决了这个问题,这不是 robospice 或 gcm 故障,而是我们服务器处理中的问题。
问题是我们从 User-Agent header 读取了一些我不知道的值,我没有将它设置到 OkHttp 客户端。