Help us understand the problem. What is going on with this article?

VolleyでCookieを設定してマルチリクエストをする方法

More than 5 years have passed since last update.

概要

VolleyでCookieを設定した状態で複数通信リクエストを行うとエラーが発生して通信が失敗する場合がある

詳細

以下のようなエラーログが出力される。

10-31 17:37:37.484: E/Volley(2448): java.lang.IllegalStateException: Adapter is detached.
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.conn.AbstractPooledConnAdapter.assertAttached(AbstractPooledConnAdapter.java:90)
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:118)
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-31 17:37:37.484: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-31 17:37:37.484: E/Volley(2448): at com.android.volley.toolbox.HttpClientStack.performRequest(HttpClientStack.java:87)
10-31 17:37:37.484: E/Volley(2448): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
10-31 17:37:37.484: E/Volley(2448): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
10-31 17:37:37.584: E/Volley(2448): [232] NetworkDispatcher.run: Unhandled exception java.lang.IllegalStateException: No wrapped connection.
10-31 17:37:37.584: E/Volley(2448): java.lang.IllegalStateException: No wrapped connection.
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.conn.AbstractClientConnAdapter.assertValid(AbstractClientConnAdapter.java:163)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.conn.AbstractClientConnAdapter.getRemotePort(AbstractClientConnAdapter.java:314)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:137)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:408)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-31 17:37:37.584: E/Volley(2448): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-31 17:37:37.584: E/Volley(2448): at com.android.volley.toolbox.HttpClientStack.performRequest(HttpClientStack.java:87)
10-31 17:37:37.584: E/Volley(2448): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
10-31 17:37:37.584: E/Volley(2448): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)

解決法

RequestQueueを生成する時にthreadPoolSizeに1を設定する

サンプル
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }

        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork(stack);

       RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network, 1);
        queue.start();

        return queue;
}

VolleyクラスのnewRequestQueueをコピーしてRequestQueueの生成部分を変えているだけです。
どうもCookie設定を設定している場合は複数のNetworkDispatchrで使用するのに問題があるようです。

参照

ネットワーク通信用ライブラリVolleyを使いこなす

http://techbooster.org/android/hacks/16474/

java.lang.IllegalStateException: Adapter is detached

http://stackoverflow.com/questions/22804643/java-lang-illegalstateexception-adapter-is-detached

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away