OkHttp 的简单使用
2017-07-05 10:41
162 查看
OKHttp的依赖
compile'com.squareup.okhttp3:okhttp:3.2.0'
compile'com.squareup.okio:okio:1.7.0'
添加的权限
<uses-permissionandroid:name="android.permission.INTERNET"/>
GET异步请求
异步POST请求
4.异步下载文件
要记得加权限
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 = < 4000 strong>"http://news.op.wpscdn.cn/uploadfile/2017/0620/20170620101507878.jpeg"[/b]; 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", "文件下载成功"); } }); }
相关文章推荐
- OKHttp的简单使用
- OKHttp的简单使用
- OkHttp——简单使用
- 关于对OKHttp的简单使用
- 关于对OKHttp的简单使用
- 关于对OKHttp的简单使用
- Http请求框架 okHttp 简单使用介绍
- okhttp的使用以及简单的封装
- OKHttp3同步和异步的简单使用方法
- 关于对OKHttp的简单使用
- OKHttp的简单使用
- OKHttp简单使用
- Okhttp的简单介绍和使用(一)
- okHttp封装,使用超简单的网络请求实现
- okhttp3的简单使用
- OKHttp的简单使用
- Okhttp的简单介绍和使用(一)
- OkHttp的简单使用
- OkHttp框架的简单使用
- Okhttp的简单介绍和使用