我创建了应用服务器,通过http post将消息发送到gcm服务器。执行代码后,我得到的响应代码为200,但显示的结果为Error=MissingRegistration
。
我无法理解为什么缺少注册ID。我已成功通过在其上创建Google帐户来获取模拟器的注册ID。我已经验证了我的项目ID和密钥,并且还使用了gcm-server.jar来创建服务器。我也尝试使用服务器密钥而不是浏览器密钥,但仍然得到相同的结果。
以下是我的应用程序服务器的代码,它通过http post将消息发送到gcm服务器。
package com.example.gcmserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
public class Server {
public static void main(String[] args) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
client.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest arg0, HttpContext context)
throws HttpException, IOException {
// TODO Auto-generated method stub
AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (state.getAuthScheme() == null) {
BasicScheme scheme = new BasicScheme();
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
if (credentials == null) {
throw new HttpException();
}
state.setAuthScope(AuthScope.ANY);
state.setAuthScheme(scheme);
state.setCredentials(credentials);
}}
}, 0);
HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("data.registration id","APA91bERSCmbVV-diaS7OkA6nQlatuq1e8whUhjN54WcsfasiY3PSlby2_DtSq7Gjs_rXcOuUPTj8obnvnD55VbXJiWvWgXGj71oRFw_0yP4uz9JGDTax5dY-DKhnk6tK2gEFy50bDuKebQoRnUo9CYfSSW3AAtqV4tJDKv4GYwtUI0vIUliSJM"));
urlParameters.add(new BasicNameValuePair("data.title", "e-pass application"));
urlParameters.add(new BasicNameValuePair("data.message", "Your code is 13133"));
httppost.setHeader("Authorization",
"key=mybrowserkey");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
httppost.setHeader("Sender_ID", "myprojectid");
httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));
HttpResponse response = client.execute(httppost);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
System.out.println("Getting the contents of the message");
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("the result is:"+result);
}
}
我已经参考了这个链接来创建服务器: Trying to HTTP-POST to GCM from Java
非常感谢您的帮助。
答案 0 :(得分:0)
无需使用&#34;数据前缀注册ID。&#34;。键值对应为:registration_ids
,包含注册ID的数组