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. B2C

    B2C是Business-to-Customer的缩写,而其中文简称为"商对客"."商对客"是电子商务的一种模式,也就是通常说的直接面向消费者销售产品和服务商业 ...

  2. C语言中string char int类型转换

    C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...

  3. 使用JSTL 对在页面上对 0,0,1 的分割处理 forTokens

    使用JSTL 对在页面上对 0,0,1 的分割处理 <tr onmouseover="currentcolor=this.style.backgroundColor;this.styl ...

  4. ACTION 的跳转与参数传递

    openmodifychildsysfunmenu <td width="54%"><a href="#" style="float ...

  5. idea项目文件名为红色的解决办法

    设置项目的版本控制为none或者对应的版本控制,如下图,添加对应项目的版本控制为none:

  6. 《Image Generation with PixelCNN Decoders》论文笔记

    论文背景:Google Deepmind团队于2016发表在NIPS上的文章 motivation:提出新的image generation model based on pixelCNN[1]架构. ...

  7. U3D中碰撞体和刚体的关系

    1.刚体是用来接受力作用的组件: 2.碰撞体是碰撞系统用来检测碰撞的组件: 碰撞产生碰撞信息,游戏物体根据碰撞信息生成一个力作用在刚体上,刚体受力后就会产生一个速率,最终在游戏物体的运动体现出来. 也 ...

  8. ubuntu 12.04 samba 配置(转载)

    转自:http://blog.sina.com.cn/s/blog_a5b607f801013av9.html 首先当然是要安装samba了,呵呵: 代码: sudo apt-get install ...

  9. idea 启动时报 error:java 无效的源发行版11

    编译的版本不符合,需要修改统一 

  10. 如何使用mysqldump命令导入导出数据库下的数据或表结构(远程or本地都适合)

    不多说,直接上干货! https://www.cloudera.com/documentation/manager/5-1-x/Cloudera-Manager-Installation-Guide/ ...