【问题标题】:GCM POST from application来自应用程序的 GCM POST
【发布时间】:2013-01-14 02:51:38
【问题描述】:

我正在尝试使用 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 服务器的响应,所以我真的不知道从这里去哪里。感谢您的帮助。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    是否可以从 android 应用程序发送这个 HTTP POST 请求?

    这就是我在 GCMintent 类中发布有关用户注册的所有相关设备信息的方式。希望这有助于回答您的问题,我不是 100% 您正在尝试做的事情。

    @Override
    protected void onRegistered(Context ctx, String regId) {
        // send regId to your server
        sendRegistrationIdToServer(regId, "01");    
    }
    
    
    public void sendRegistrationIdToServer(String registrationId, String func) {
        //Send the reg info to server
        Log.d(TAG, "Sending registration ID to my application server");
        String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);
        Context context = getApplicationContext();
    
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId));
            nameValuePairs.add(new BasicNameValuePair("func", func));
            nameValuePairs.add(new BasicNameValuePair("registrationid",registrationId));
            nameValuePairs.add(new BasicNameValuePair("app", GetPackageName()));
    
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    
            String line = "";
            while ((line = rd.readLine()) != null) {
                Log.e("HttpResponse", line);
            }
    
        } catch (IOException e) {
            Log.e("HttpResponse", "broke");
        }
    }
    

    它可以很容易地适应发送Json。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      相关资源
      最近更新 更多