Okhttp3发送xml、json、文件的请求方法
1、引入依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.1</version>
</dependency>
1
2
3
4
5
2、加入工具类
package znxd.video.bank.base;
import okhttp3.*;
import java.io.File;
/**
* http请求工具类
*/
public class Okhttp3Utils {
/**
* xml格式post请求接口调用
* @param url 接口地址
* @param xmlStr xml格式请求参数体
* @return
*/
public static String postXml(String url,String xmlStr){
RequestBody body=RequestBody.create(MediaType.parse("application/xml"),xmlStr);
Request requestOk = new Request.Builder()
.url(url)
.post(body)
.build();
Response response;
try {
response = new OkHttpClient().newCall(requestOk).execute();
String jsonString = response.body().string();
if(response.isSuccessful()){
return jsonString;
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return "";
}
/**
* get请求接口,返回json
* @param url 接口地址
* @return
*/
public static String getJson(String url){
// RequestBody body=RequestBody.create(MediaType.parse("application/json"),"");
Request requestOk = new Request.Builder()
.url(url)
.get()
.build();
Response response;
try {
response = new OkHttpClient().newCall(requestOk).execute();
String jsonString = response.body().string();
if(response.isSuccessful()){
return jsonString;
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return "";
}
/**
* 发送文件
* @param url 请求接口地址
* @param uploadDir 参数上传目录
* @param baseFileUrl 文件保存基准路径
* @param relativeUrl 文件保存的相对路径
* @return 接口返回值
* 该方法前端以formData格式传入,包括文件和参数,可一起传入。
*/
public String uploadFilePost(String url,String uploadDir,String baseFileUrl,String relativeUrl){
File temporaryFile = new File(baseFileUrl+relativeUrl);
if(!temporaryFile.exists()){
return "";
}
RequestBody requestBody = new MultipartBody.Builder()
.addFormDataPart("uploadDir", uploadDir) //参数一,可注释掉
.addFormDataPart("fileUrl", relativeUrl) //参数二,可注释掉
.addFormDataPart("file", temporaryFile.getName(), RequestBody.create(MediaType.parse("application/octet-stream"),temporaryFile)) //文件一
.build();
Request requestOk = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response;
try {
response = new OkHttpClient().newCall(requestOk).execute();
String jsonString = response.body().string();
// temporaryFile.delete();
if(response.isSuccessful()){
return jsonString;
}
} catch (Exception e) {
e.printStackTrace(http://www.my516.com);
}
return "";
}
}
---------------------
Okhttp3发送xml、json、文件的请求方法的更多相关文章
- 一文综述python读写csv xml json文件各种骚操作
Python优越的灵活性和易用性使其成为最受欢迎的编程语言之一,尤其是对数据科学家而言.这在很大程度上是因为使用Python处理大型数据集是很简单的一件事情. 如今,每家科技公司都在制定数据战略. ...
- nginx 缓存,大文件分片请求方法
实现的途径:expire cache-control 更新缓存的机制 如何校验本地缓存是否过期 expires cache-control(max-age)如果超期,说明失效 然后进行etag是否过期 ...
- npm install 不更改 package-lock.json 文件的解决方法
package-lock.json 文件是版本锁定文件 package-lock.json 是在 `npm install` 时候生成的一份文件,用以记录当前状态下实际安装的各个 npm packag ...
- jmeter 如何发送上传文件接口请求
1.上传图片接口,通过抓包工具获取接口相关信息,然后在信息头里添加Content-Disposition:form-data; name="imgType" 2.在请求中MIME类 ...
- IIS 7启用static JSON文件能POST方法
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- 发送xml报文去第三方请求获取xml报文数据
import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import j ...
- JAVA发送xml格式的接口请求
/** * * @param urlStr 接口地址 * @param xmlInfo xml格式参数数据 * @return */ public static String sendMsgXml(S ...
- Solr json,xml等文件数据导入(添加索引)linux下操作
使用solr-5.3.1\example\exampledocs下的post.jar来完成数据导入 1.将想要导入的文件放在solr-5.3.1\example\exampledocs中,如aaa.x ...
- nodejs 如何发送一个带JSON的GET请求?
GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" ...
随机推荐
- java 编码设置
- ubuntu12.04安装tftp,配置,修改目录,错误类型
[前言]学习嵌入式,需要配置tftp服务,在网上搜了搜,很多,但是配置了,我的老是出现Error code 1: File not found错误,经过探索和一个大哥的博客http://blog.cs ...
- URAL1099 Work Scheduling —— 一般图匹配带花树
题目链接:https://vjudge.net/problem/URAL-1099 1099. Work Scheduling Time limit: 0.5 secondMemory limit: ...
- UVALive3126 Taxi Cab Scheme —— 最小路径覆盖
题目链接:https://vjudge.net/problem/UVALive-3126 题解: 最小路径覆盖:即在图中找出尽量少的路径,使得每个结点恰好只存在于一条路径上.其中单独一个点也可以是一条 ...
- POJ3177 Redundant Paths —— 边双联通分量 + 缩点
题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total ...
- bzoj-1192 [HNOI2006]鬼谷子的钱袋(水题)
题目链接: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各 ...
- Hardcoded string "下一步", should use @string resource警告 (转载)
转自:http://blog.csdn.net/iqv520/article/details/7579513 在布局文件中,文本的设置使用如下写法时会有警告:Hardcoded string &quo ...
- Ubuntu 12.04的gnome classic panel添加程序快捷键(转载)
How to add applets to the Gnome classic panel in Ubuntu 12.04 转自:http://www.borfast.com/blog/how-add ...
- 4 Java 如何判定是否存活或者死亡
在堆中存放着几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事就是要确定这些对象之中哪些还活着,哪些对象已经死去. 判断对象是否已经死亡有以下几种算法: 引用计数法算法 定义 : 给对象中添加一 ...
- [App Store Connect帮助]七、在 App Store 上发行(2.5)设定价格与销售范围:向企业或教育机构分发您的 App
您可以向您添加至 App Store Connect 以及参与“Apple 商务”或“校园教务管理”的用户.企业或教育机构单独分发您的非公开 App.此选项仅在您的 App 尚未获得批准时可用. 有关 ...