14

OkHttp2.0 不再支持这个 OkHttpStack: https ://gist.github.com/JakeWharton/5616899

当前将 OkHttp 2.0.0 与 Volley 集成的模式是什么?

4

3 回答 3

30

您必须使用实现 java.net.HttpURLConnection API 的 okhttp-urlconnection 模块,因此:

  • 下载或设置okhttp-urlconnection的依赖项

  • 重写 OkHttpStack 以使用 OkUrlFactory 类:

    public class OkHttpStack extends HurlStack {
       private final OkUrlFactory okUrlFactory;
       public OkHttpStack() {
           this(new OkUrlFactory(new OkHttpClient())); 
       }
       public OkHttpStack(OkUrlFactory okUrlFactory) {
           if (okUrlFactory == null) {
               throw new NullPointerException("Client must not be null.");
           }
           this.okUrlFactory = okUrlFactory;
       }
       @Override
       protected HttpURLConnection createConnection(URL url) throws IOException {
           return okUrlFactory.open(url);
       }
    }

于 2014-07-02T12:39:00.607 回答
5

你也可以用这个

import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;

/**
 * An {@link com.android.volley.toolbox.HttpStack HttpStack} implementation
 * which uses OkHttp as its transport.
 */
public class OkHttpStack extends HurlStack {
    private final OkUrlFactory mFactory;

    public OkHttpStack() {
        this(new OkHttpClient());
    }

    public OkHttpStack(OkHttpClient client) {
        if (client == null) {
            throw new NullPointerException("Client must not be null.");
        }
        mFactory = new OkUrlFactory(client);
    }
}
于 2014-07-25T08:56:52.680 回答
2

您现在也可以在不依赖 HttpURLConnection 的情况下执行此操作:

https://plus.google.com/+JakeWharton/posts/31jhDwaCvtg

https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750

于 2015-02-23T20:36:20.180 回答