2013-08-31 51 views
2

我想从开发人员Android网站了解GCM。按照http://developer.android.com/google/gcm/client.html和http服务器应用程序(而不是基于XMPP的服务器应用程序)的说明,按照http://developer.android.com/google/gcm/http.html上的说明执行了客户端Android应用程序。我使用的代码是从https://code.google.com/p/gcm/下载的,因为他们已经提到过。 GCM注册功能在我的手机上完美运行。如何从Android应用程序调用服务器应用程序servlet注册设备时,实施GCM

现在的问题是,我如何将我的手机的注册ID发送到我的http服务器应用程序。我知道我应该将一些代码放在android应用程序的DemoActivity.java文件的sendRegistrationIdToBackend()中,以便简单地在我的服务器应用程序上调用RegisterServlet。但我是新来的Java和Android,只是无法弄清楚如何做到这一点。任何关于如何编写此代码的建议都将受到高度赞赏。

回答

4

下面是使用HTTP GET请求将注册ID发送到服务器的示例代码。我正在使用org.apache.http.*库的类。它假定您的服务器上有一个页面,该页面接受名为regId的参数中的注册ID(示例中它是一个jsp页面,但它可以是您服务器中任何内容的PHP)。您必须添加错误处理代码并解析服务器响应才能完成此示例。

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 
    } 
+0

工作正常!因为我在Web服务器上使用servlet来处理注册请求,所以我在HTTP GET请求中使用'URI url = new URI(“http:// server-domain/register?regId =”+ THE_REGISTRATION_ID);''。并确保在我的服务器应用程序的web中有 RegisterServlet /register .xml文件。 – faizal

+0

你是如何实现你的http服务器以便它接受注册ID的? – 2014-06-03 15:41:14

+0

@ user2320244在这里回答太长了。你应该首先选择你希望实现你的服务器的语言(Java Servlet,JSP,PHP,.NET等)。 – Eran

相关问题