实现GCM时如何从android应用程序调用服务器应用程序servlet来注册设备 [英] how to call server app servlet from android app to register device when implementing GCM
问题描述
我正在尝试从开发者 android 站点了解 GCM.我已经按照 http://developer.android.com 上的说明实现了客户端 android 应用程序/google/gcm/client.html 和 http 服务器应用程序(不是基于 XMPP 的服务器应用程序)按照 http://developer.android.com/google/gcm/http.html.我使用的代码是从 https://code.google.com/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 服务器应用程序. 我知道我应该在 DemoActivity.java 文件的 sendRegistrationIdToBackend() 中放置一些代码android 应用程序的简单调用我的服务器应用程序上的 RegisterServlet.但我是 java 和 android 的新手,只是不知道如何做到这一点.任何关于我如何编写此代码的建议将不胜感激.
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.
推荐答案
这是使用 HTTP GET 请求将注册 ID 发送到您的服务器的示例代码.我正在使用 org.apache.http.*
库的类.它假设您的服务器上有一个页面,该页面接受名为 regId 的参数中的注册 ID(在示例中,它是一个 jsp
页面,但它可以是 PHP
在您的服务器中).您必须添加错误处理代码和解析服务器响应才能完成此示例.
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
}
这篇关于实现GCM时如何从android应用程序调用服务器应用程序servlet来注册设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!