简体   繁体   中英

Fastest android server communication

I am creating an android application to get information from a server as fast as possible. I am not interested in security or preserving battery life. The messages will most likely be small but will come in fast (every few seconds). Communication will primarily be uni directional however the ability for the application to communicate with the server would be an added bonus.

I have been looking at Google Cloud Messaging (GCM) however there are mixed reports regarding the speed of this.

How does this compare in terms of speed to say a HTTP/JSON connection? or setting up a socket that the server would connect and push the message to?

Are there any other alternatives I have not considered?

EDIT: This will running exclusively over WiFi

套接字io提供与服务器的持续连接,因此速度非常快(每次连接都不会松动)。

You can achieve this by using a concept called Long Polling

A typical implementation is as follows:

    @Override
    protected String doInBackground(String... arg0) {
     String result= TIME_OUT;   //public static final String TIME_OUT = time_out_error" 
     while(result.equals(TIME_OUT))
         result = getServerInformation();

     return result;
    }




public String  getServerInformation(){
         String result = null;
        DefaultHttpClient def = new DefaultHttpClient();
        HttpParams httpParams = def.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);

        ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpPost httpPost = new HttpPost(mPushURL);
        httpPost.addHeader("Accept", "application/json");

        try {
            Log.i(TAG, "Executing POST(PUSH) request " + httpPost.getRequestLine());

            HttpResponse httpResponse = def.execute(httpPost);
            Log.i(TAG, result);
            Log.i(TAG, String.valueOf(httpResponse.getProtocolVersion()));
            Log.i(TAG, String.valueOf(httpResponse.getEntity().getContent())); //For testing purposes


        } catch (ClientProtocolException e) {
                 e.printStackTrace();
       } catch (IOException e) {
            e.printStackTrace();
        }
//HERE YOU SHOULD TURN result = TIME_OUT or whatever you want
        return result;

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM