2011-12-27 11 views
0

バックエンドサーバーへの一連のHTTPS接続を行う必要があるアプリがあります。これらの接続はユーザーの入力に応じて行われるため、HTTSセッションを再利用することが可能です。他のプラットフォーム(iOS)での実験では、このアプローチによって「ロード」の進行時間が数秒短縮されることが示されています。しかし、私はAndroidの良いアプローチを見つけていません。アイデア?AndroidでHTTPS接続を再利用しますか?

ここには現在のアプローチが簡略化されていますが、依然として長いです。 (申し訳ありません!)

public class ExampleActivity extends Activity { 
static final String preconnectEndpoint; // value redacted -- pick your favorite https url. 
static final String TAG = "HttpsReuseExampleActivity"; 

private Button startButton; 
private TextView resultText; 

private HttpClient httpClient; 

private void setupHttpClient(Context androidContext) { 
    if (httpClient == null) { 
     httpClient = AndroidHttpClient.newInstance("reusableHttpsConnectionExampleClient (Android/0.1)", androidContext); 
     Log.i(TAG,"created httpClient" + httpClient.toString()); 

     SSLSessionCache sslSession = new SSLSessionCache(androidContext); 
     SSLSocketFactory sslSocketFactory = SSLCertificateSocketFactory.getHttpSocketFactory(10*60*1000, sslSession); 

     ClientConnectionManager cm = httpClient.getConnectionManager(); 
     Log.d(TAG, "ClientConnectionManager is: " + cm.toString()); 
     cm.getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443)); 
    } 
} 

    private class ConnectTask extends AsyncTask<Void, Void, String>{ 
    private HttpClient client; 
    private long connectionStartTime; 

    ConnectTask(HttpClient client) { 
     this.client = client; 
    } 

    @Override 
    protected void onPreExecute() { 
     Log.d(TAG, "beginning connection..."); 
     connectionStartTime = System.currentTimeMillis(); 
    } 

    @Override 
    protected String doInBackground(Void... arg0) { 
     Log.d(TAG, "connecting with client: " + client.toString()); 

     try { 
      HttpGet getRequest = new HttpGet(preconnectEndpoint); 
      HttpResponse resp = client.execute(getRequest); 
     } catch (Exception e) { 
      Log.e(TAG,"failed to connect", e); 
      return "connection failed"; 
     } 

     return String.format("connected after %d ms", System.currentTimeMillis() - connectionStartTime); 
    } 

    @Override 
    protected void onPostExecute(String r) { 
     Log.i(TAG,r); 
     resultText.setText(r); 
    } 

}; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    startButton = (Button) findViewById(R.id.startButton); 
    resultText = (TextView) findViewById(R.id.resultText); 

    setupHttpClient(this.getApplicationContext()); 

    startButton.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      assert(httpClient != null); 
      ConnectTask t = new ConnectTask(httpClient); 
      t.execute(); 
     } 
    }); 
} 

答えて

1

それがキャッシュされたスレッドプールを使用しているためhttp://loopj.com/android-async-http/は、優れたソリューションを提供していることが判明しました。 AsyncTaskはおそらく同様のことをするために変更/サブクラス化できますが、なぜホイールを再発明するのでしょうか?

接続が一定の条件の下でキャンセルされなければならないので、私はhttps://github.com/loopj/android-async-http/commit/c7745276853ecd2e3838655f3ef93e683e80723d

で提供される更新を必要ことがわかった
関連する問題