标签: okhttp

OkHTTP和Picasso不会一起运行

我在我的项目中使用Picasso库来加载图像和缓存它们.它没有任何问题,效果很好.但是,当我尝试使用OkHttp库与我的服务器(JSON通信)进行数据通信时,Picasso会抛出异常.

我使用以下罐子:okhttp-2.0.0-RC2,okio-1.0.0,picasso-2.2.0.当我添加这些罐子后运行我的项目时,它崩溃了以下内容:

06-12 11:13:15.824: E/dalvikvm(12105): Could not find class 'com.squareup.okhttp.HttpResponseCache', referenced from method com.squareup.picasso.OkHttpDownloader.<init>
Run Code Online (Sandbox Code Playgroud)

我添加了okhttp只是为了使用以下方法:

public static String executeHttpGet(String urlStr) {
    Response response = null;
    String result = "";
    OkHttpClient client = new OkHttpClient();

    try {
        Request request = new Request.Builder().url(urlStr).build();

        response = client.newCall(request).execute();
        result = response.body().string();
    } catch (Exception ex) {

    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有任何问题.然而,使用Picasso库并且用于完美工作的代码开始抛出以下例外:

06-12 11:19:49.307: E/AndroidRuntime(13036): FATAL EXCEPTION: main
06-12 11:19:49.307: E/AndroidRuntime(13036): java.lang.NoClassDefFoundError: com.squareup.okhttp.HttpResponseCache
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:74)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:51)
06-12 11:19:49.307: …
Run Code Online (Sandbox Code Playgroud)

android picasso okhttp

20
推荐指数
3
解决办法
2万
查看次数

为什么我应该使用OkHttp而不是android httpClient和AsyncTask

在SpeakerDeck(https://speakerdeck.com/pareshmayani/lazy-android-developers-be-productive)的Paresh Mayani演讲中,他说最好使用OkHttpRetrofit代替AsyncTask使用DefaultHttpClient.

我的问题是为什么?
为什么他们更快?
那些也是基于默认android类的库吗?
OkHttp和Retrofit有什么区别?

谢谢!

android android-networking androidhttpclient retrofit okhttp

20
推荐指数
1
解决办法
1万
查看次数

改造 - Okhttp客户端如何缓存响应

我正在尝试缓存由Retrofit(v 1.9.0)和OkHttp(2.3.0)完成的http调用的响应.如果我尝试在没有互联网的情况下拨打电话,它总是会拨打网络电话java.net.UnknownHostException.

RESTClient实现

public class RestClient {
public static final String BASE_URL = "http://something.example.net/JSONService";
private com.ucc.application.rest.ApiService apiService;

public RestClient() {
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .create();

    RequestInterceptor requestInterceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json");
            int maxAge = 60 * 60;
            request.addHeader("Cache-Control", "public, max-age=" + maxAge);
        }
    };

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(BASE_URL)
            .setClient(new OkClient(OkHttpSingleTonClass.getOkHttpClient()))
            .setConverter(new GsonConverter(gson))
            .setRequestInterceptor(requestInterceptor)
            .build();

    apiService = restAdapter.create(com.ucc.application.rest.ApiService.class);
}

public com.ucc.application.rest.ApiService getApiService() {
    return apiService;
} …
Run Code Online (Sandbox Code Playgroud)

java android cache-control retrofit okhttp

20
推荐指数
1
解决办法
2万
查看次数

使用Volley和OkHttp时如何配置Http Cache?

我想尝试将Volley与OkHttp结合使用,但是Volley缓存系统和OkHttp都依赖于HTTP规范中定义的HTTP缓存.那么如何禁用OkHttp的缓存以保留一份HTTP缓存?

编辑:我做了什么

public class VolleyUtil {
    // http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
    private volatile static RequestQueue sRequestQueue;

    /** get the single instance of RequestQueue **/
    public static RequestQueue getQueue(Context context) {
        if (sRequestQueue == null) {
            synchronized (VolleyUtil.class) {
                if (sRequestQueue == null) {
                    OkHttpClient client = new OkHttpClient();
                    client.networkInterceptors().add(new StethoInterceptor());
                    client.setCache(null);
                    sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
                    VolleyLog.DEBUG = true;
                }
            }
        }
        return sRequestQueue;
    }
}
Run Code Online (Sandbox Code Playgroud)

OkHttpClient是从引用https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750

android caching android-volley okhttp

20
推荐指数
1
解决办法
1313
查看次数

Android Volley MalformedURLException错误的URL

在使用第二个网络请求后Volley,我总是收到此错误.我输入的网址是什么似乎并不重要. Volley总是声称它是畸形的.

08-04 20:16:26.885  14453-14470/com.thredup.android E/Volley? [994] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL
java.lang.RuntimeException: Bad URL
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:127)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
 Caused by: java.net.MalformedURLException: Protocol not found:
        at java.net.URL.<init>(URL.java:176)
        at java.net.URL.<init>(URL.java:125)
        at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
Run Code Online (Sandbox Code Playgroud)

进一步调查,我在HurlStack中放了几个日志.在

public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders),
Run Code Online (Sandbox Code Playgroud)

失败的请求是REQUEST [ ] 0x0 LOW 26."

因此,HurlStack的第101行: URL parsedUrl = new URL(url);

以空URL(request.getUrl()为空)失败.

我正在使用OkHttpStack(扩展HurlStack).关于可能导致这种情况的任何想法?

android android-networking android-volley okhttp

19
推荐指数
2
解决办法
2万
查看次数

如何在从站点下载和解析XML文件时一起使用Retrofit和SimpleXML?

我刚开始使用Retrofit.我正在开发一个使用SimpleXML的项目.有人可以为我提供一个示例,其中一个从站点获取XML,例如http://www.w3schools.com/xml/simple.xml "并将其读出来?

android simple-framework retrofit okhttp okio

19
推荐指数
1
解决办法
2万
查看次数

滑动 - 添加标头以请求

是否有一种方法可以在下载图像时添加自定义标题以供请求?我可以在Glide中使用volley或okhttp.

我尝试在okhttpclient中向cookiemanager添加cookie,但它没有帮助.有没有一种方法可以在Glide中调试请求响应?

最好的问候汤姆

android request android-volley okhttp android-glide

19
推荐指数
4
解决办法
2万
查看次数

图像适配器泄漏内存

我有一个简单的ListActivity,显示图像和我inizialize我OkHttpClient毕加索生成器在ImageAdapter类的构造函数:

picassoClient = new OkHttpClient();
picassoClient.interceptors().add(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request newRequest = chain
            .request()
            .newBuilder()
            .addHeader("Cookie","xyz")
            .build();

        return chain.proceed(newRequest);
    }
});

new Picasso.Builder(context).downloader(new OkHttpDownloader(picassoClient)).build();
Run Code Online (Sandbox Code Playgroud)

然后在getView()我使用Picasso在ImageView中加载图像:

Picasso.with(context).load(xyzUrl).fit().centerCrop().into(vImage);
Run Code Online (Sandbox Code Playgroud)

它运行良好,但在设备的旋转中,我看到堆大小有时会慢慢增长,有时很快,有时会保持稳定.它很少掉落.我是在泄漏内存还是在代码中出现了问题?

编辑: 我在Picasso的电话中插入了这段代码getView()

if (BuildConfig.DEBUG) {
    Log.i("HEAP SIZE",
    String.valueOf((Runtime.getRuntime().totalMemory() / 1024)
    - (Runtime.getRuntime().freeMemory() / 1024)));
}
Run Code Online (Sandbox Code Playgroud)

我发现堆大小的增长发生在getView()后加载位图到ImageView中.怎么了?

编辑2:尝试设置静态ImageAdapter,没有任何变化

编辑3: 尝试使用RecyclerView而不是ListView,行为相同:堆大小不断增长,同时滚动图像列表每次步进30-40个字节onBindViewHolder().设备的旋转堆大小有时会增加甚至2-3兆字节.很少下降.

为什么堆大小缓慢但持续增长?为什么我在设备轮换后泄漏一些缓存或一些缓存的位图?

更新: 尝试了没有构造函数中的代码的适配器(没有new OkHttpClientnew Picasso.Builder),它工作,堆大小现在保持稳定.那么,使用cookie头管理初始化客户端的正确方法是什么? …

android listadapter picasso okhttp android-recyclerview

19
推荐指数
1
解决办法
1885
查看次数

如何通过改造和rxjava取消任务

我有休息api.

@Get("/serveraction")
public Observable<String> myRequest(@Query("Data") String data);
Run Code Online (Sandbox Code Playgroud)

我知道,okhttp已取消功能(通过请求对象,按标签),但不知道如何使用它与改造和rxjava.使用改造和rxjava实现网络任务取消机制的最佳方法是什么?

reactive-programming rx-java retrofit okhttp

19
推荐指数
2
解决办法
9097
查看次数

OkHttp - 启用日志

我使用Retrofit来发出Http请求和Json解析,我喜欢打开调试日志的方法.日志允许查看正文请求,网址...这非常有用.由于Retrofit使用OkHttp,我想知道OkHttp是否也有办法为每个请求启用日志.

android web-services http okhttp

18
推荐指数
4
解决办法
2万
查看次数