2013-08-31 49 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

相關問題