OkHttp框架的应用
来源:互联网 发布:淘宝卖家常用插件工具 编辑:程序博客网 时间:2024/06/01 20:20
通过OkHttp实现基本的GET,POST等相关的请求:
OkHttp使用一般分为四个步骤:以Get请求为例
/** * 1.拿到okHttpClient对象 * 2.构造Request * 3.将Request封装为Call * 4.执行Call */
我们写的Get请求方法:
public void doGet(View view) throws IOException { //构造OkHttpClient OkHttpClient okHttpClient = new OkHttpClient(); //构造Request Request request = new Request.Builder() .get() .url(mBaseUrl + "login?username=get&password=12223") .build(); //执行Call executeRequest(request); }其中对3,4步骤封装在了executeRequest方法中 ,具体如下:
private void executeRequest(Request request) { Call call = okHttpClient.newCall(request); //Response execute = call.execute(); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { L.e("onFailure:" + e.getMessage()); e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { L.e("onResponse"); final String res = response.body().string(); L.e(res); runOnUiThread(new Runnable() { @Override public void run() { tv.setText(res); } }); } }); }
先建立Call对象,执行Call.execute或Call.enqueue方法。
这里要注意的是onResponse方法其实是工作在子线程中。
这里mBaseUrl
private String mBaseUrl = "http://本机ip地址/Okhttp/"url格式如下:
url(mBaseUrl + "login?username=get&password=12223")
Okhttp为我们服务器的项目名,login为服务器端的方法名,username和password为参数。这样我们就可以完成前后端的交互。
我们在服务端需要建立Okhttp的Web Project,同时搭建本地服务器,具体的搭建方式如下:搭建服务器Tomcat
我们建立UserAction的java源文件
建立login方法
public String login() throws IOException{System.out.println(username + " , " +password);HttpServletResponse response = ServletActionContext.getResponse();PrintWriter writer = response.getWriter();writer.write("login success");writer.flush();return null;}得到Response对象来与客户端交互,在writer中写入了login success,那么在客户端的executeRequest的onResponse回调方法()中得到Response对象,并将login success显示在TextView中。
服务端写完login方法后还要在struts.xml中声明一下:
重启服务器,并运行AndroidStudio代码:
login success便显示在了TextView中。
其他的一些方法大体步骤相同,比如PostFile方法是将一个文件上传到服务器端
public void doPostFile(View view) { //得到外存图片文件 File file = new File(Environment.getExternalStorageDirectory(), "shaozuo.jpg"); if (!file.exists()) { L.e(file.getAbsolutePath() + "not exist! "); return; } L.e("find pic"); //构造RequstBody RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file); //构造Request Request request = new Request.Builder() .url(mBaseUrl + "postFile") .post(requestBody) .build(); executeRequest(request); }这里我们在构造Request时,需要构造RequestBody,一般Post请求时都要构造RequestBody,构造RequestBody时,需要传入文件的MIME type参数。
服务器端的代码如下:
public String postFile() throws IOException{HttpServletRequest request = ServletActionContext.getRequest();ServletInputStream is = request.getInputStream();String dir = ServletActionContext.getServletContext().getRealPath("files");File file = new File(dir, "shaozuo.jpg");FileOutputStream fos = new FileOutputStream(file);byte[] buf = new byte[1024];int len = 0;while( (len = is.read(buf)) != -1 ){fos.write(buf, 0, len);}fos.flush();fos.close();return null;}这里服务端接受到的图片就会存放在 dir中,Okhttp\files文件夹下
运行客户端代码,当我们单击POST FILE 按钮时,我们的服务端路径下就会多一张图片,是客户端发送来的。
okhttp的使用方法具体的就是4个步骤
* 1.拿到okHttpClient对象 * 2.构造Request * 3.将Request封装为Call * 4.执行Call
不同请求方法也只是一些具体的细节不同(比如构造RequestBody方法不同。。),就不一一介绍了。
阅读全文
0 0
- OkHttp框架的应用
- okhttp框架解析与应用
- okhttp框架的封装
- okhttp框架的使用
- OKHttp框架的使用
- Okhttp框架的使用
- okhttp简单的应用
- OkHttp框架缓存的缺陷
- OkHttp框架的简单使用
- 关于OkHttp框架的使用方法
- okhttp网络框架的使用
- OkHttp网络框架的使用
- OkHttp缓存功能的应用
- oKhttp框架
- OkHTTP框架
- OKHttp框架
- OkHttp框架
- OkHttp框架
- Gym
- 如何实现字符串的反转及替换?
- sql语句的优化分析
- C/C++ 输出宽字符 printf + %ls or wcout
- python中set集合用法
- OkHttp框架的应用
- Android学习日志——第14天
- Spring boot 整合Dubbo
- Shell脚本基础13-case练习举例
- windows单机创建多个服务,用于模拟主从
- 宠物商店 案例分析
- java图解数据结构--树状结构
- Xshell5 连接Virtualbox中Ubuntu虚拟机(详细教程)
- Android基础——5:ListView及RecyclerView