OKHttp基本使用
2016-11-03 01:17
477 查看
一.原生OKHttp的Get和Post请求
特点:1.回调仍然不在UI线程
[b]
MyStringCallback.java
2.使用okhttp-utils的post请求网络文本数据
3.使用okhttp-utils文件下载
4.使用okhttp-utils上传多个或者单个文件
5.使用okhttp-utils请求图片---不是它的强项glide较好
特点:1.回调仍然不在UI线程
[b]
1 public class MyStringCallback extends StringCallback { 2 @Override 3 public void onBefore(Request request, int id) { 4 setTitle("loading..."); 5 } 6 7 @Override 8 public void onAfter(int id) { 9 setTitle("Sample-okHttp"); 10 } 11 12 @Override 13 public void onError(Call call, Exception e, int id) { 14 e.printStackTrace(); 15 tv_result.setText("onError:" + e.getMessage()); 16 } 17 18 @Override 19 public void onResponse(String response, int id) { 20 Log.e(TAG, "onResponse:complete"); 21 tv_result.setText("onResponse:" + response); 22 23 switch (id) { 24 case 100: 25 Toast.makeText(OKHttpActivity.this, "http", Toast.LENGTH_SHORT).show(); 26 break; 27 case 101: 28 Toast.makeText(OKHttpActivity.this, "https", Toast.LENGTH_SHORT).show(); 29 break; 30 } 31 } 32 33 @Override 34 public void inProgress(float progress, long total, int id) { 35 Log.e(TAG, "inProgress:" + progress); 36 mProgressBar.setProgress((int) (100 * progress)); 37 } 38 }
MyStringCallback.java
2.使用okhttp-utils的post请求网络文本数据
url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api"; OkHttpUtils .post() .url(url) .id(100) .build() .execute(new MyStringCallback());
3.使用okhttp-utils文件下载
String url = "http://vfx.mtime.cn/Video/2016/07/24/mp4/160724055620533327_480.mp4"; OkHttpUtils// .get()// .url(url)// .build()// .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "okhttp-utils-test.mp4")// { @Override public void onBefore(Request request, int id) { } @Override public void inProgress(float progress, long total, int id) { mProgressBar.setProgress((int) (100 * progress)); Log.e(TAG, "inProgress :" + (int) (100 * progress)); } @Override public void onError(Call call, Exception e, int id) { Log.e(TAG, "onError :" + e.getMessage()); } @Override public void onResponse(File file, int id) { Log.e(TAG, "onResponse :" + file.getAbsolutePath()); } });
4.使用okhttp-utils上传多个或者单个文件
String mBaseUrl = "http://192.168.0.165:8080/FileUpload/FileUploadServlet"; File file = new File(Environment.getExternalStorageDirectory(), "afu.png"); File file2 = new File(Environment.getExternalStorageDirectory(), "test.txt"); if (!file.exists()||!file2.exists()) { Toast.makeText(OKHttpActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show(); return; } Map<String, String> params = new HashMap<>(); params.put("username", "ganchuanpu"); params.put("password", "123"); String url = mBaseUrl ; OkHttpUtils.post()// .addFile("mFile", "server_ww.png", file)// .addFile("mFile", "server_test.txt", file2)// .url(url) .params(params)// .build()// .execute(new MyStringCallback());
5.使用okhttp-utils请求图片---不是它的强项glide较好
String url = "http://images.csdn.net/20150817/1.jpg"; OkHttpUtils .get()// .url(url)// .tag(this)// .build()// .connTimeOut(20000) .readTimeOut(20000) .writeTimeOut(20000) .execute(new BitmapCallback() { @Override public void onError(Call call, Exception e, int id) { tv_result.setText("onError:" + e.getMessage()); } @Override public void onResponse(Bitmap bitmap, int id) { Log.e("TAG", "onResponse:complete"); iv_icon.setImageBitmap(bitmap); } });
相关文章推荐
- okhttp基本使用
- 《第一行代码》第二版 学习总结25 OkHttp基本使用
- OkHttp基本使用
- Android okhttp3的基本使用
- OkHttp的基本使用(二)Post请求
- OKHTTP3 基本使用
- OkHttp的基本使用(一)Get请求
- Android okhttp3的基本使用
- OkHttp的基本使用
- okhttp3的基本使用
- OkHttp,Retrofit 1.x - 2.x 基本使用
- 网络框架okHttp之基本使用
- Retrofit+okhttp基本使用
- OkHttp基本使用
- OkHttp的基本使用——替代Apache HttpClient
- OkHttp使用(一)基本使用
- OkHttp基本使用
- OkHttp的基本使用-2(使用最简单的方式)
- OkHttp的基本使用——替代Apache HttpClient
- OkHttp的基本使用——替代Apache HttpClient