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攻击手段极其防御方式 总结几种常见的安全算法 回顾 总结几个概念(具体描述可以看上一篇文章) 数字 ...
随机推荐
- mysql插入、更新与删除
数据库增删改查都是要熟练掌握的. 这部分就来看看前面3个比较简单的部分,增,删,改. 插入数据 为表的所有字段插入数据 insert into table_name (column_list) val ...
- [ubuntu]E: The package firmware-upgrade needs to be reinstalled, but I can't find an archive for it.
解决办法把firmware-upgrade卸载 sudo dpkg --remove --force-all firmware-upgrade 然后 sudo apt-get update 即可
- ContikiMAC RDC协议
http://www.mamicode.com/info-detail-1348767.html
- 配置TOMCAT 修改默认ROOT路径
本文转载http://xxs673076773.iteye.com/blog/1134805 最合适的) 最直接的办法是,删掉tomcat下原有Root文件夹,将自己的项目更名为Root. 我在$to ...
- oozie中调度mapreduce
mapreduce可以直接对hdfs进行清洗和计算,这里介绍oozie中如何调度使用. 操作步骤如下: 1. 写一个mapper和reduce类,并且打包成jar包 2. 在workflow中引用ma ...
- Yii 中Criteria常用方法
$criteria = new CDbCriteria; //select $criteria->select = '*';//默认* $criteria->select = 'id,na ...
- javascript 图片滚动
<div style="width:9999px;"> <ul id="marquePic1_1"> <li> <dl ...
- am335x watchdog
am335x watchdog 内核文档kernel/Documentation/watchdog Qt@aplex:~/kernel/7109/linux-3.2.0/Documentation/w ...
- udhcpc
/********************************************* * dhcpc * dhcpc是dhcp的客户端,在busybox中实现.今天正好了解一下. * Tony ...
- ubuntu环境JDK安装(转至 http://hi.baidu.com/leo_lovato/item/31d1150d31a06d8002ce1bec)
ubuntu安装jdk 1.首先去官网http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载最新版的jdk.我下载了 ...