与volley和okhttp3的Android网络问题与过时的信息

时间:2017-07-08 22:08:37

标签: android android-volley android-networking okhttp3

通过阅读各种在线博客/教程, volley + okhttp + gson 似乎是最佳选择。但由于在线过时/冲突的信息,我无法启动和运行简单的网络项目。有人可以帮我解决一下吗?

This outdated article说为了使用okhttp和volley,我需要为okhttp使用httpstack的自定义实现。所以我发现并使用了这个updated okhttpstack version for okhttp3因为okhttp3现在是最新的。

但是当我将它加载到我的项目中时,会发生以下错误:

1)okhttp3stack中不兼容的类型

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;

// ...

@Override
public HttpResponse performRequest(com.android.volley.Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    // ...

    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    // ...

    return response;
}

private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    // ...
    return entity;
}

Android Studio抱怨错误:

Incompatible types: 
    Required: HttpResponse
    Found: BasicHttpResponse

Incompatible types:
    Required: HttpEntity
    Found: BasicHttpEntity

这是什么?我应该更改函数定义以返回BasicHttpResponse,还是更改变量以返回HttpResponse?同样,我应该怎么做http实体错误?

2)它正在使用传统的apache http库。

从文档中,我被告知 HttpURLConnection 是我应该使用的那个。当我使用过时的apache http库运行它时,我看到了:

Error:(30, 23) error: package org.apache.http does not exist
Error:(31, 23) error: package org.apache.http does not exist
Error:(32, 23) error: package org.apache.http does not exist
Error:(33, 23) error: package org.apache.http does not exist
Error:(34, 30) error: package org.apache.http.entity does not exist
Error:(35, 31) error: package org.apache.http.message does not exist
Error:(36, 31) error: package org.apache.http.message does not exist
Error:(37, 31) error: package org.apache.http.message does not exist
Error:(62, 12) error: cannot find symbol class HttpResponse
Error:(105, 20) error: cannot find symbol class HttpEntity
Error:(159, 20) error: cannot find symbol class ProtocolVersion
Error:(90, 9) error: cannot find symbol class StatusLine
Error:(90, 41) error: cannot find symbol class BasicStatusLine
Error:(91, 9) error: cannot find symbol class BasicHttpResponse
Error:(91, 42) error: cannot find symbol class BasicHttpResponse
Error:(98, 40) error: cannot find symbol class BasicHeader
Error:(106, 9) error: cannot find symbol class BasicHttpEntity
Error:(106, 38) error: cannot find symbol class BasicHttpEntity
Error:(162, 28) error: cannot find symbol class ProtocolVersion
Error:(164, 28) error: cannot find symbol class ProtocolVersion
Error:(166, 28) error: cannot find symbol class ProtocolVersion
Error:(168, 28) error: cannot find symbol class ProtocolVersion

各种博客说只是将apache http库添加到gradle文件中,但我不想这样做,因为它将被弃用。由于HttpURLConnection是当前的迭代,我想使用它。我如何更新它以使用 HttpURLConnection

3)在某处有评论说okhttp3stack不能用于HTTPS。我不太明白这意味着什么。如果确实如此,我将如何支持HTTPS呢?

基本上,作为一个完整的Android菜鸟,我对如何在Android中进行简单的网络完全感到困惑。我的最终目标是向外部API发出GET / POST / PUT请求。

有人可以帮助让它正常工作,或使用更新/现代网络库展示一个完整的工作示例吗?

0 个答案:

没有答案