how to call server app servlet from android app to register device when implementing GCM

前端 未结 1 1576
无人共我
无人共我 2021-01-03 14:45

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/goog

相关标签:
1条回答
  • 2021-01-03 15:08

    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
      }
    
    0 讨论(0)
提交回复
热议问题