我正在尝试使用 Google Cloud Messaging 在有新数据要从服务器接收时通知用户。使用 GCM 发送消息定义为:
To send a message, the application server issues a POST request to https://android.googleapis.com/gcm/send.
是否可以从 android 应用程序发送这个 HTTP POST 请求?我尝试使用我一直成功使用的方法来实现这一点,以 POST 到我由 MongoLab 托管的服务器。我的尝试如下:
input_json.put("registration_ids", RegistrationIDs);
input_json.put("data", data);
param = new StringEntity(input_json.toString());
HttpParams httpParams = new BasicHttpParams();
int some_reasonable_timeout = (int) (20 * DateUtils.SECOND_IN_MILLIS);
HttpConnectionParams.setConnectionTimeout(httpParams, some_reasonable_timeout);
HttpConnectionParams.setSoTimeout(httpParams, some_reasonable_timeout);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(url);
request.setEntity(param);
request.setHeader("Content-type", "application/json");
request.setHeader("Authorization", API_KEY);
HttpResponse response = httpClient.execute(request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_OK){
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = responseHandler.handleResponse(response);
}
我没有收到任何 HTTP 错误或来自 gcm 服务器的响应,所以我真的不知道从这里去哪里。谢谢你的帮助。