我是Java编程的新手。我开发了一个Android应用程序,它向GCM服务器发送请求,并作为响应,它获取一个注册ID,我发送到我的Java服务器和服务器上,我将它存储在数据库中。它的工作正常:)
对于第二部分,我在java服务器中编写了http代码。使用此代码,我从数据库访问注册ID并将其发送到GCM服务器,其中包含用于设备的字符串消息。但我收到了错误。我的http客户端代码在这里。请帮助一下吗?
public static String REQUEST_URL = "https://android.googleapis.com/gcm/send";
private static String GCM_ID = "APA91bFEnZXT7YRTJm2d5NpbKcpJCuDwEIJjZmwYITIIWIOFIZHEtwYc8S3oN_Upe3RnSvUqcwmntIrMqaF8Vn04b_EZtvoDQMAqt21Zw9Sb9GhNXwVS70yDOjUyRohH7u1RmtTHRrkjaBUueIjS2gD1Uw1cQDJbQg";
/**
* @param args
*/
public static void main(String[] args) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("registration_id", GCM_ID));
formparams.add(new BasicNameValuePair("data", "this is data mesg"));
// UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
// "UTF-8");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(REQUEST_URL);
httpPost.setHeader("Authorization",
"key=AIzaSyBTgKVWevp0GwM5m2QAuF__39eEI0bclVA");
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
try {
httpPost.setEntity(new UrlEncodedFormEntity(formparams, "utf-8"));
httpclient.execute(httpPost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
我不知道你有什么错误,但这一行:
formparams.add(new BasicNameValuePair("data", "this is data mesg"));
应该是:
formparams.add(new BasicNameValuePair("data.message", "this is data mesg"));
您在GCM邮件中包含的每个有效内容参数都应具有data.<key>
形式的名称。