Okhttp常用方法示例
这是我用到的一个util类
public class HttpBaseService { private OkHttpClient client = new OkHttpClient();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public String sendGetRequest(String url){
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
} public int sendDelRequest(String url){
Request request = new Request.Builder().url(url).delete().build();
return sendOperateRequest(request);
} public int sendPostRequest(String url,String json){
// RequestBody formBody = new FormEncodingBuilder()
// .add("platform", "android")
// .add("name", "bug")
// .add("subject", "XXXXXXXXXXXXXXX")
// .build();
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder().url(url).post(body).build();
return sendOperateRequest(request);
}
public int sendPutRequest(String url,String json){
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder().url(url).put(body).build();
return sendOperateRequest(request);
} private int sendOperateRequest(Request request){
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return ;
}
} catch (IOException e) {
e.printStackTrace();
}
return -;
}
}
Okhttp常用方法示例的更多相关文章
- Guava的常用方法示例
Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...
- delphi webbrowser 常用方法示例
var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...
- python 基础 1.5 python数据类型(四)--字典常用方法示例
一. 字典 #字典 dict1 = {'name':'lzc','age':'20','sex':'man'} print dict1 print type(dict1) >>> { ...
- python 基础 1.5 python数据类型(三)--元组常用方法示例
#/usr/bin/python#coding=utf-8#@Time :2017/10/13 15:02#@Auther :liuzhenchuan#@File :元组.py #tuple() 字符 ...
- python 基础 1.5 python数据类型(二)--列表常用方法示例
#/usr/bin/python #coding=utf-8 #@Time :2017/10/12 23:30 #@Auther :liuzhenchuan #@File :列表.py lis ...
- java操作redis(jedis)常用方法示例
说明:redis命令和jedis方法名基本是一一对应的 Redis常用命令1 连接操作命令 ● quit:关闭连接(connection) ● auth:简单密码认证 ● help cmd: 查看cm ...
- 我的 Android 学习笔记-Okhttp 的使用(译)
okhttp 已经是非常流行的网络请求库了.网上介绍的文章非常之多,但感觉都不是特别系统.遂想到官方应该有介绍的文档,仔细寻找一番,果然.但可惜是英文的,于是就大致翻译了一下,权当做笔记了. 1.Ca ...
- java 数据类型:Stream流 对象转换为集合collect(Collectors.toList()) ;常用方法count,limit,skip,concat,max,min
集合对象.stream() 获取流对象,对元素批处理(不改变原集合) 集合元素循环除了用for循环取出,还有更优雅的方式.forEach 示例List集合获取Stream对象进行元素批处理 impor ...
- C#基础篇 - 正则表达式入门
1.基本概念 正则表达式(Regular Expression)就是用事先定义好的一些特定字符(元字符)或普通字符.及这些字符的组合,组成一个“规则字符串”,这个“规则字符串”用来判断我们给定的字符串 ...
随机推荐
- ItemsControl
<ItemsControl Grid.Row=" ItemsSource="{Binding Content.patientInfoList}" Width=&qu ...
- IIS-网站发布之后访问HTTP 错误 403.14 - Forbidden
这种问题一般是因为页面本身发生了错误的原因导致的,这个时候先开启[目录浏览]功能 开通目录浏览之后再重新访问,就能看到相应的错误了,再去进行相应的解决就可以了.
- Android-Bundle认知、和Intent的差别
不时的回过头来看看自己的Andriod学习.实践之路,总发现有些曾经不明确的,如今清楚缘由.也会发现一些之前没怎么关注的.如今看到了 ,很想去深刻了解的. 比方:Bundle. 在一个Activity ...
- 纯 html 以及 js 多域名跳转
<!--将以下的 endv.cn 改成要跳转的域名-->第一种:单域名的跳转 1:域名在服务器端跳转 Response.Redirect(http://endv.cn) Response. ...
- SQL Server 附加数据库提示5120错误
怎么样是不是跟你的错误是一样的,心里是不是有点小激动? T_T 终于有办法了!!!! 第一步先关掉你的SQLserver 然后在菜单上找找到SQLSERVER右键选择“以管理员运行” 第二步给你的数据 ...
- ThinkPHP3.2 新bug ReadHtmlCache 支持不区分大写和小写的函数
报错提示: Fatal error: Function name must be a string in D:\wwwroot\zbphp.com\ThinkPHP\Library\Behavior\ ...
- Office EXCEL VBA数组如何使用
Excel VBA数组入门教程 1. 前言:不要把VBA数组想的太神秘,它其实就是一组数字而已. 2. 数组的维数: Sub 数组示例() Dim x As Long, y As Long Dim ...
- swift学习之元组
元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...
- 微信小程序 - 自定义swiper(dot)指示点
点击下载示例:自定义swiper(dot)指示点
- spring jdbc连接数据库
1.在applicationContext.xml中配置jdbc bean <bean id="dataSource" class="org.springframe ...