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、文件的请求方法的更多相关文章

  1. 一文综述python读写csv xml json文件各种骚操作

      Python优越的灵活性和易用性使其成为最受欢迎的编程语言之一,尤其是对数据科学家而言.这在很大程度上是因为使用Python处理大型数据集是很简单的一件事情. 如今,每家科技公司都在制定数据战略. ...

  2. nginx 缓存,大文件分片请求方法

    实现的途径:expire cache-control 更新缓存的机制 如何校验本地缓存是否过期 expires cache-control(max-age)如果超期,说明失效 然后进行etag是否过期 ...

  3. npm install 不更改 package-lock.json 文件的解决方法

    package-lock.json 文件是版本锁定文件 package-lock.json 是在 `npm install` 时候生成的一份文件,用以记录当前状态下实际安装的各个 npm packag ...

  4. jmeter 如何发送上传文件接口请求

    1.上传图片接口,通过抓包工具获取接口相关信息,然后在信息头里添加Content-Disposition:form-data; name="imgType" 2.在请求中MIME类 ...

  5. IIS 7启用static JSON文件能POST方法

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  6. 发送xml报文去第三方请求获取xml报文数据

    import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import j ...

  7. JAVA发送xml格式的接口请求

    /** * * @param urlStr 接口地址 * @param xmlInfo xml格式参数数据 * @return */ public static String sendMsgXml(S ...

  8. Solr json,xml等文件数据导入(添加索引)linux下操作

    使用solr-5.3.1\example\exampledocs下的post.jar来完成数据导入 1.将想要导入的文件放在solr-5.3.1\example\exampledocs中,如aaa.x ...

  9. nodejs 如何发送一个带JSON的GET请求?

    GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" ...

随机推荐

  1. (22) java web的struts2框架的使用-struts配置文件

    1,配置文件的引用 struts中配置文件可以有多个,每个模块的包里面都可以单独设立一个struts配置文件. 主的配置文件,放在“src”文件夹下,可以引入其他配置文件,引入方式: <!-- ...

  2. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  3. 获取Android APK JNI库

    /************************************************************************** * 获取Android APK JNI库 * 说 ...

  4. openpyxl操作excel

    [转] openpyxl库可以读写xlsx格式的文件,对于xls旧格式的文件只能用xlrd读,xlwt写来完成了. python有很多模块都是用来操作excel的,比如xlrd,xlwt,pyExce ...

  5. 去除inline-block的间隙

    产生间隙的原因就是标签之间的空格,去除的方法: 1 设置父元素的font-size:0;空格字符的宽高都为0, <div class="demo1 demo2"> &l ...

  6. ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(五)外借/阅览图书信息的增删改查

    前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/asp ...

  7. ios手机Safari本地服务连不上

    问题: 今天在本地起服务准备测下ios手机端页面,结果发现:页面可以打开,但是登录不上. 用alert定位了下,await fn() 报错被try()catch(){}捕获了... 原因: 该机子不支 ...

  8. 进击的Python【第九章】:paramiko模块、线程与进程、各种线程锁、queue队列、生产者消费者模型

    一.paramiko模块 他是什么东西? paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 先来个实例: import param ...

  9. 【洛谷2304_LOJ2134】[NOI2015]小园丁与老司机(动态规划_网络流)

    题目: 洛谷 2304 LOJ 2134 (LOJ 上每个测试点有部分分) 写了快一天 -- 好菜啊 分析: 毒瘤二合一题 -- 注意本题(及本文)使用 \(x\) 向右,\(y\) 向上的「数学坐标 ...

  10. 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table

    题目传送门 /* 水题:读懂题目就能做 */ #include <cstdio> #include <iostream> #include <algorithm> ...