web基础---->okhttp的使用
今天我们就讲一下okhttp的使用,具体的okhttp使用可以参见官方的文档。
okhttp的使用
一、okhttp的下载安装
- Download the latest JAR or grab via Maven:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.5.0</version>
</dependency>
- 或者使用Gradle:在build.properties的dependencies下添加:
compile 'com.squareup.okhttp3:okhttp:3.5.0'
二、okHttp的基础使用
- get的同步请求:The string() method on response body is convenient and efficient for small documents. But if the response body is large (greater than 1 MiB), avoid string() because it will load the entire document into memory. In that case, prefer to process the body as a stream.
@Test
public void okHttpTest1() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/ParameterServlet").build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- get的异步请求:Download a file on a worker thread, and get called back when the response is readable. The callback is made after the response headers are ready. Reading the response body may still block. OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:9999/huhx1.json").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("hello wrold fail");
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
} @Override
public void onFailure(Call call, IOException exception) {
exception.printStackTrace();
}
});
- post请求
@Test
public void okHttpTest3() {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder().add("username", "huhx").add("password", "123456").build();
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/ParameterServlet").post(formBody).build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- json格式的post请求
@Test
public void okHttpTest4() {
MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
json.put("username", "huhx");
json.put("password", "123456");
RequestBody requestBody = RequestBody.create(JSON_TYPE, json.toJSONString());
Request request = new Request.Builder().url("http://localhost:8080/TomcatClient/JsonRequestServlet").post(requestBody).build();
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
String string = body.string();
System.out.println("string " + string);
body.close();
} catch (IOException e) {
e.printStackTrace();
}
}
友情链接
- okhttp的官方文档: https://github.com/square/okhttp/wiki
web基础---->okhttp的使用的更多相关文章
- Golang友团无闻Go语言Web基础视频教程
教程内容:GO语言资料Golang友团无闻Go语言编程基础Golang友团无闻Go语言Web基础教程 Go语言Web基础教程列表:[Go Web基础]12Go Web 扩展学习.mp4[Go Web基 ...
- HT for Web基础动画介绍
在上一篇<基于HT for Web矢量实现3D叶轮旋转>一文中,我略微提了下HT for Web基础动画的相关用法,但是讲得不深入,今天就来和大家分享下HT for Web基础动画的相关介 ...
- Web基础开发最核心要解决的问题
Web基础开发要解决的问题,往往也就是那些框架出现的目的 - 要解决问题. 1. 便捷的Db操作: 2. 高效的表单处理: 3. 灵活的Url路由: 4. 合理的代码组织结构: 5. 架构延伸 缓存. ...
- web基础--html
WebBasic 1.web应用体系 课程大纲 1.web基础:做网页 2.结构: a.html 勾勒网页结构及内容 b.css ...
- java web基础环境搭建
java web基础环境包括:(1)servlet容器也即tomcat(2)jre即java程序运行环境 环境变量配置:分别下载jdk和tomcat安装包. jdk环境变量配置: 第一步:系统环境变量 ...
- Web基础知识和技术
WEB是一个外延广泛的概念,不单单指网站,乌徒帮专注拥有WEB界面的网站开发,帮助初学者或已经进入开发的朋友们提供参考讨论平台,然而并不一定能将所有的WEB知识讲全讲透,只是能满足初涉者的建站需求,能 ...
- java web基础 --- URL重定向Filter
java web基础 --- URL重定向Filter httpRequest.getRequestDispatcher("/helloWorld").forward(httpRe ...
- (0)写给Web初学者的教案-----Web基础
0,Web基础 一. What is the Web? Can It Eat? 很多同学可能都听说过一个名词叫做“Web”,这个词隐隐约约好像和我们上网相关.但是呢,又很难说的清楚.我们今天每位 ...
- web基础系列(五)---https是如何实现安全通信的
https是如何实现安全通信的 如果有不正确的地方,还望指出! web基础系列目录 总结几种常见web攻击手段极其防御方式 总结几种常见的安全算法 回顾 总结几个概念(具体描述可以看上一篇文章) 数字 ...
随机推荐
- Object中有哪些方法?
有一种遗憾就是,每天都见到你,但是却不关注你!等到面试的时候才后悔莫及. Object类中有9大public方法: equals:判断两个对象"相等" hashCode:获取对象的 ...
- C语言 · 字符串逆序
算法训练 字符串逆序 时间限制:1.0s 内存限制:512.0MB 输入一个字符串,长度在100以内,按相反次序输出其中的所有字符. 样例输入 tsinghua 样例输出 auhgn ...
- Android——Button的颜色
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Yii 中Criteria常用方法
$criteria = new CDbCriteria; //select $criteria->select = '*';//默认* $criteria->select = 'id,na ...
- Lua中的loadfile、dofile、require详解
1.loadfile——只编译,不运行 loadfile故名思议,它只会加载文件,编译代码,不会运行文件里的代码.比如,我们有一个hellofile.lua文件: 复制代码代码如下: print(“h ...
- 内网DNS投毒技术劫持会话
工具列表: tcpdump Ferret Hamster node closurether 拓扑环境: 攻击机:Kali 10.10.10.237 被攻击机: win7 10.10.10.232 因为 ...
- js 动态设置 option 的selected 选项
思路:通过for循环判断每个选项,一旦满足条件则设置其selected属性为true即可,关键代码: var obj = document.getElementById(select_id); for ...
- 资深投资人全力反击: VC增值平台从来就不是一坨狗屎
编者注: 本文来自海外著名科技博客VentureBeat, 英文原文出自Kyle Lacy之手 ,中文版由天地会珠海分舵进行编译. 文章主要是针对前几天德国VC Christian Claussen的 ...
- arugsJS 入门
一款优秀的前端框架——AngularJS 前 言 AngularJS是一款为了克服HTML在构建应用上的不足而设计的优秀的前端JS框架.AngularJS有着诸多特性,最为核心的是:MVC. ...
- informatica中的workflow连接远程数据库
如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl