OkHttp 的简单使用

OKHttp的依赖
compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okio:okio:1.7.0'
添加的权限

<uses-permissionandroid:name="android.permission.INTERNET"/>

GET异步请求
public void getAsynHttp() {
    //创建okHttpClient对象
    OkHttpClient mOkHttpClient = new OkHttpClient();
    final Request request = new Request.Builder()
            .url(url)
            .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            str = response.body().string();
            Log.i("wangshu", str);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Gson gson = new Gson();
                    HomeBean homeBean = gson.fromJson(str, HomeBean.class);
                    List<HomeBean.DataBean> data = homeBean.getData();
                    LvAdapter lvAdapter = new LvAdapter(MainActivity.this, data);
                    lv.setAdapter(lvAdapter);
                    Toast.makeText(getApplication(), "请求成功", Toast.LENGTH_SHORT).show();
                }
            });
        }

    });

}
异步POST请求
private void postAsynHttp() {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormBody.Builder()
            .add("size", "10")
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            str1 = response.body().string();
            Log.i("wangshu", str1);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Gson gson = new Gson();
                    HomeBean homeBean = gson.fromJson(str1, HomeBean.class);
                    List<HomeBean.DataBean> data = homeBean.getData();
                    LvAdapter lvAdapter = new LvAdapter(MainActivity.this, data);
                    lv.setAdapter(lvAdapter);
                    Toast.makeText(getApplicationContext(), "Post请求成功", Toast.LENGTH_SHORT).show();
                }
            });
        }

    });
}
4.异步下载文件
要记得加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

private void downAsynFile() {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    String url = "http://news.op.wpscdn.cn/uploadfile/2017/0620/20170620101507878.jpeg";
    Request request = new Request.Builder().url(url).build();
    mOkHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) {
            InputStream inputStream = response.body().byteStream();
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(new File("/sdcard/wangshu.jpg"));
                byte[] buffer = new byte[2048];
                int len = 0;
                while ((len = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                fileOutputStream.flush();
            } catch (IOException e) {
                Log.i("wangshu", "IOException");
                e.printStackTrace();
            }

            Log.d("wangshu", "文件下载成功");
        }
    });
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值