2016-07-13 24 views
0

HTTP 2.0ベースのクライアントを実装するためにokhttp(3.4.1)を使用しようとしています。私の要件の1つは、応答を後で処理するコールバックを使用して、複数の非同期HTTP要求を異なるURLに実装することです。 しかし、私の現在の実装では、最初にメインスレッドからHTTPリクエストをブロックしない限り、すべての非同期リクエストが同じTCP接続を使用することはできません。 私は、非同期呼び出しに使用されるenque()メソッドがディスパッチャを利用して、各リクエストに対して新しいスレッドを生成しているようだと思います。okhttp3で複数の非同期HTTP 2.0リクエストを作成する

public void AsyncGet(String url) throws Exception { 

    Request request = new Request.Builder().url(url).build(); 

    Call call = client.newCall(request); 

    call.enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      e.printStackTrace(); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      if (!response.isSuccessful()) { 
       throw new IOException("Unexpected code " + response); 
      } 
      new Thread() { 
       @Override 
       public void run() { 
        /* Some code */ 
       } 
      }.start(); 
     } 
    }); 

} 

私の同期GETリクエストを次のとおりです。:

public Response SyncGet(String url) throws Exception { 

    Request request = new Request.Builder().url(url).build(); 

    Call call = client.newCall(request); 
    Response response = call.execute(); 

    if (!response.isSuccessful()) { 
     throw new IOException("Unexpected code " + response); 
    } 

    return response; 
} 

呼び出しシーケンスなどを作り、次のように

OkHttpClient client = new OkHttpClient(); 

マイ非同期GETリクエスト方法が見えます: はここに私のコードスニペットです次のようにすると、サーバーへの2つのTCP接続がトリガーされます。

AsyncGet(Url); 
AsyncGet(Url2); 

ただし、次のような呼び出しシーケンスでは、同じTCP接続が使用されます。

SyncGet(Url); 
AsyncGet(Url); 
AsyncGet(Url2); 

私はこれをデバッグしていないが、それはOkHttpはおそらくTCP接続コンテキストを取得するために最初のメインスレッド上のブロッキングHTTPリクエストを行い、その後、他のスレッドでそれを共有するために私たちを強制的にのように見えますか?または、私は何かを逃していますか?

+0

はhttp://stackoverflow.com/questions/32625035/when-using-http2-in-okhttp-why-multi-requests-to-the-same-host-didnt-と同じ問題のように見えますuse-just – vijaygos

答えて

0

HIコールの応答を区別できるセクタに基づいて、非同期コールを呼び出してセクタを後で各コールに設定できます。このコードの下にしようと、それはuの後、API呼び出し

パブリッククラスRestApi {

protected RestApiResponse serverResponse; 
protected Context c; 

public RestApi(RestApiResponse serverResponse, Context c) { 
    this.serverResponse = serverResponse; 
    this.c = c; 
} 

public void postDataWithoutAuth(String url,String sector) { 


    OkHttpClient client = new OkHttpClient(); 

    Request request = new Request.Builder() 
.url("http://publicobject.com/helloworld.txt") 
.build(); 

    client.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      Log.e("response", call.request().body().toString()); 
      serverResponse.onErrorLoaded(call.request().body().toString(), sector); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      serverResponse.onResponseLoaded(response, sector); 
     } 


    }); 
} 

}

  • コールバック

    用クレートのインターフェイスをため

    • クレート別のクラスに役立つことを願っています

    パブリックインターフェイスRestApiResponse {

    public void onResponseLoaded(Response response, String sector); 
    
    public void onErrorLoaded(String response, String sector); 
    

    }

    • 及び活動から、このようなアクセス

      RestApi apicall =新しいRestApi(この、getBaseContext())。 apicall.postDataWithoutAuthUrlEncoded( "ur url"、 "your sector");

  • +0

    あなたの答えをありがとう。私はアンドロイド用のプロトタイプを開発していないと付け加えなければならない。これは、okhttpを使った純粋なデスクトップアプリケーションを意味します。私にとっては、okhttpの現在のディスパッチャモジュールは、現在この機能が不足しているようです(私の質問で述べたようなハックを作成しない限り)。 – vijaygos

    関連する問題