热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android网络请求框架okHttp3

okhttpGitHub地址:https:github.comsquareokhttp最新的OkHttp依赖可以到官方的GitHub上去添加。目前官方的依赖地址为&

okhttp GitHub地址:
https://github.com/square/okhttp
最新的OkHttp依赖可以到官方的GitHub上去添加。

目前官方的依赖地址为:
compile ‘com.squareup.okhttp3:okhttp:3.6.0’


Get请求

直接进入主题:

// 1、拿到OKHttpClient对象OkHttpClient okHttpClient=new OkHttpClient();// 2、构造Request//Request是构造者模式,所以使用的时候要.BuilderRequest.Builder builder=new Request.Builder();Request reqyest=builder.get().url("http://www.baidu.com").build();// 3、将Request封装为CallCall call=okHttpClient.newCall(reqyest);// 4、执行Call/* Response response=call.execute(); //直接执行 需要抛出异常*/call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//失败以后的方法}@Overridepublic void onResponse(Call call, Response response) throws IOException {//成功以后回调的方法String res=response.body().string(); //回调一个字符串}});

最后在Manifests中打开网络权限即可使用Get请求请求网络数据了。
get请求头的格式:
例: url+”login?username=chengdu&password=123456”


POST请求

post请求大体步骤和GET请求差不多,具体如代码所示

//获得一个功能强大的FormBody表单RequestBody formBody=new FormBody.Builder().build();//2.构造RequestRequest.Builder builder=new Request.Builder();Request request=builder.url("http://www.imooc.com/").post(formBody).build();// 3、将Request封装为CallexecuteRequest(request);

RequestBody的数据格式都要指定Content-Type,常见的有三种:


  • application/x-www-form-urlencoded 数据是个普通表单
  • multipart/form-data 数据里有文件
  • application/json 数据是个json

之后还要加上 charset=utf-8

POST Json例子

public void postJson(){//获得一个FormBody表单RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),"{Json字符串}");//2.构造RequestRequest.Builder builder=new Request.Builder();Request request=builder.url("http://www.imooc.com/").post(requestBody).build();// 3、将Request封装为CallexecuteRequest(request);}

POST 上传一个图片

public void postFile(){File file=new File(Environment.getExternalStorageDirectory(),"banana.jpg");if (!file.exists()){Log.e("-------","不存在");return;}//获得一个FormBody表单RequestBody requestBody=RequestBody.create(MediaType.parse("application/octet-stream; charset=utf-8"),file);//2.构造RequestRequest.Builder builder=new Request.Builder();Request request=builder.url("http://www.imooc.com/").post(requestBody).build();// 3、将Request封装为CallexecuteRequest(request);}

下载文件

public void downloadFile(){Request.Builder builder=new Request.Builder();Request request=builder.get().url("所要下载文件的文件路径").build();Call call=okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//失败以后的方法Log.e("----","失败以后的方法");}@Overridepublic void onResponse(Call call, Response response) throws IOException {InputStream inputStream=response.body().byteStream();int len=0;File file=new File(Environment.getExternalStorageDirectory(), "zc.jpg");byte[] buf =new byte[128];FileOutputStream fos=new FileOutputStream(file);while ((len= inputStream.read(buf))!=-1){fos.write(buf,0,len);}fos.flush();fos.close();inputStream.close();}});

下载图片并且设置

//下载图片并且设置public void downloadSetImage(){Request.Builder builder=new Request.Builder();Request request=builder.get().url("所要下载文件的文件路径").build();Call call=okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//失败以后的方法Log.e("----","失败以后的方法");}@Overridepublic void onResponse(Call call, Response response) throws IOException {InputStream inputStream=response.body().byteStream();final Bitmap bitmap= BitmapFactory.decodeStream(inputStream);runOnUiThread(new Runnable() {@Overridepublic void run() {mImageView.setImageBitmap(bitmap);}});}});}

推荐阅读
author-avatar
琪琪格
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有