• 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 "";
    }
    }


    --------------------- 

  • 相关阅读:
    Data Guard相关参数学习介绍
    Android打包失败Proguard returned with error code 1. See console
    Extjs4 类的定义和扩展
    c#中常用的异常类型
    cocos2d-x过程动作CCProgressTo示例学习笔记
    欧拉函数
    sae上屏蔽错误显示并查看错误日志
    在wdcp环境下架设VSFTPD虚拟用户只上传功能服务器
    Android开发匹配字符笔记
    setImageResource与setImageBitmap的区别
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11007495.html
Copyright © 2020-2023  润新知