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" ...
随机推荐
- caioj1230: [图论补充]哈密顿路径
保存模版 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> ...
- silverlight漂亮的文件上传进度显示原理及示例
silverlight漂亮的文件上传进度显示原理及示例 作者:chenxumi 出处:博客园 2009/11/27 13:37:11 阅读 1219 次 概述:在网站根目录web.config里配 ...
- 【T^T 1736】【FJUTOJ 1077】排座位
http://59.77.139.92/problem.php?id=1077 水题,小心PE // <1736.cpp> - 11/12/16 17:17:52 // This file ...
- I.MX6 逻辑分析仪 UART
/*********************************************************************** * I.MX6 逻辑分析仪 UART * 说明: * ...
- 并不对劲的AC自动机
这像是能解决所有问题的样子(并不).AC自动机之所以叫AC自动机是因为它能解决所有AC自动机的题. 其实只能解决的是很多模式串匹配一个母串的问题. 把kmp中的next数组得到下一次跳转的位置看成特殊 ...
- [Usaco2009 Dec] 过路费
[题目链接] https://www.luogu.org/problemnew/show/P2966 [算法] SPFA最短路 时间复杂度 : O(N ^ 2) [代码] #include<bi ...
- gunicorn部署Flask服务
作为一个Python选手,工作中需要的一些服务接口一般会用Flask来开发. Flask非常容易上手,它自带的app.run(host="0.0.0.0", port=7001)用 ...
- artemplate include
include用于嵌入字模板 {{include 'template_name'}} 子模板 默认共享当前的数据 也可以自己指定数据 {{include 'template_name' templat ...
- Tensorflow卷积接口总结
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 这个接口用了这么久,每次都有点迷惑,这里 ...
- UI:网络请求
JSON 外层是一个数组或者字典 富文本(相对来说比较安全).超文本,https安全超文本协议 NSURL NSURL *url = [[NSURL alloc]initWithString:@&qu ...