0

我是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();
    }

}
4

1 回答 1

0

我不知道您遇到了什么错误,但是这一行:

formparams.add(new BasicNameValuePair("data", "this is data mesg"));

应该 :

formparams.add(new BasicNameValuePair("data.message", "this is data mesg"));

您包含在 GCM 消息中的每个有效负载参数都应该有一个格式为 的名称data.<key>

于 2013-07-24T11:39:14.293 回答