Okhttp3发送xml、json、文件的请求方法

  如果想了解xml与字符串之间的转换可以参考这个,JAVA工具类总结——xml字符串与对象的转换
  Okhttp3发送请求的写法比较简单,使用前先引入依赖,然后在使用方法即可。
  以下仅仅包括post方式发送xml字符串get请求获取jsonpost请求发送文件及参数这三种方法。

1、引入依赖

 <dependency>
    <groupId>com.squareup.okhttp3groupId>
      <artifactId>okhttpartifactId>
      <version>3.3.1version>
  dependency>

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();
        }
        return "";
    }
}



你可能感兴趣的:(java,okhttp3发送请求例子,okhttp3发送文件,okhttp3发送xml)