OkHttp 的简单使用

来源:互联网 发布:网络回路后果 编辑:程序博客网 时间:2024/05/21 06:51
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", "文件下载成功");        }    });}


原创粉丝点击