「开源框架」OkHttp3 使用

作者: dongbingliu | 来源:发表于2017-09-05 21:28 被阅读176次
    Okhttp3

    OkHttp3 简单使用

    1. AS 项目 build.gradle 中添加:
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    
    1. AndroidManifest.xml 添加网络权限
    <uses-permission android:name="android.permission.INTERNET"/>
    
    1. Okhttp3 get 代码请求
    //创建 OkHttp 对象
    private final OkHttpClient client = new OkHttpClient();
    
    //创建 Request 对象,通过内部类 Builder 调用生成 Request 对象
    Request request = new Request.Builder()
                    .url("https://www.sogou.com/")
                    .build();
    
    //创建 Call 对象,调用 execute(同步请求)/enquene(异步请求)
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d(TAG, "onFailure: ");
        }
    
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d(TAG, "onResponse: "+response.body().string());
        }
    });
    
    1. Okhttp3 post 代码请求,( //todo)

    Okhttp3 GitHub 官方链接:https://github.com/square/okhttp

    相关文章

      网友评论

        本文标题:「开源框架」OkHttp3 使用

        本文链接:https://www.haomeiwen.com/subject/noupjxtx.html