如何调用服务器应用程序的servlet从Android应用程序实施GCM时,需要注册设备 [英] how to call server app servlet from android app to register device when implementing GCM

查看:163
本文介绍了如何调用服务器应用程序的servlet从Android应用程序实施GCM时,需要注册设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解从开发者的Andr​​oid网站GCM。我已经实现在 http://developer.android.com客户端Android应用程序下面的说明/google/gcm/client.html 和HTTP服务器的应用程序(而不是基于XMPP服务器应用程序),在之后的 http://developer.android.com/google/gcm/http.html 。 //$c$c.google.com:使用的$ C $词是从 HTTPS下载/ P /因为他们已经提到的GCM / 。该GCM注册功能完美的作品在我的手机上。

I am trying to understand GCM from the developer android site. I have implemented the client side android app following the instructions at http://developer.android.com/google/gcm/client.html and the http server app(NOT the XMPP based server app) following the instructions at http://developer.android.com/google/gcm/http.html. The code i used is downloaded from https://code.google.com/p/gcm/ as they have mentioned. The GCM registration function works perfectly on my phone.

现在的问题是,我怎么送我的手机的注册ID,以我的HTTP服务器应用程序。我知道我应该把一些code在sendRegistrationIdToBackend()在DemoActivity Android应用程序的.java文件只需调用RegisterServlet在我的服务器上的应用程序。但我是新来的Java和Android,而只是无法弄清楚如何做到这一点。我如何可以去写这篇code将是非常美联社preciated任何意见。

Now the problem is, how do i send the registration id of my phone to my http server app. I know that i should be placing some code in sendRegistrationIdToBackend() in the DemoActivity.java file of the android app to simply call the RegisterServlet on my server app. But i am new to java and android, and just cannot figure out how to do that. Any advice on how i can go about writing this code would be highly appreciated.

推荐答案

下面是一个示例$ C $下使用HTTP GET请求发送的注册ID为您的服务器。我使用的类 org.apache.http的。* 库。它假定你有你的服务器上的页面,接受一个名为REGID参数的注册ID(样品中它是一个的jsp 页,但它可以是 PHP 的无论你在你的服务器上)。你必须添加错误处理code和服务器响应的解析,以完成此示例。

Here's a sample code for sending the registration ID to your server using an HTTP GET request. I'm using classes of the org.apache.http.* library. It assumes you have a page on your server that accepts the registration ID in a parameter called regId (in the sample it's a jsp page, but it can be PHP of whatever you have in your server). You'll have to add error handling code and parsing of the server response in order to complete this sample.

  String responseString= null;

  try {
    URI url            = new URI ("http://your-server-domain/your-server-page.jsp?regId="+THE_REGISTRATION_ID);
    HttpGet httpGet    = new HttpGet (url);
    // defaultHttpClient
    HttpParams
      httpParameters   = new BasicHttpParams();

    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int
      timeoutConnection= 3000;
    HttpConnectionParams.setConnectionTimeout (
      httpParameters,
      timeoutConnection
                         );

    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket  = 5000;
    HttpConnectionParams.setSoTimeout (
      httpParameters,
      timeoutSocket
                         );

    DefaultHttpClient
     httpClient        = new DefaultHttpClient (httpParameters);

    HttpResponse
      httpResponse     = httpClient.execute (httpGet);
    HttpEntity
      httpEntity       = httpResponse.getEntity ();

    if (httpResponse.getStatusLine().getStatusCode() != 200)
    {
      Log.e (
        _context.getString(R.string.app_name),
        "Server Call Failed : Got Status Code " + httpResponse.getStatusLine().getStatusCode() + " and ContentType " + httpEntity.getContentType().getValue()
                         );
      // add code to handle error
    }

    responseString     = EntityUtils.toString (httpEntity);
  } catch (UnsupportedEncodingException e) {
    Log.e(_context.getString(R.string.app_name),e.toString(),e);
    // add code to handle error
  } catch (ClientProtocolException e) {
    Log.e(_context.getString(R.string.app_name),e.toString(),e);
    // add code to handle error
  } catch (IOException e) {
    Log.e(_context.getString(R.string.app_name),e.toString(),e);
    // add code to handle error
  } catch (URISyntaxException e) {
    Log.e(_context.getString(R.string.app_name),e.toString(),e);
    // add code to handle error
  }

这篇关于如何调用服务器应用程序的servlet从Android应用程序实施GCM时,需要注册设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆