使用okhttp进行接口测试

package com.woood.okhttp;

import okhttp3.*;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class Test {

    OkHttpClient client = new OkHttpClient();
    /**
     * post使用的json头
     */
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");


    /**
     * get接口操作方法     *
     *
     * @param url 接口地址
     * @return
     * @throws IOException
     */
    public String get(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("GET");
            return response.body().string();
        } else {
            throw new IOException("错误码: " + response);
        }
    }

    /**
     * 使用键值对调用post方法
     * @param url 接口链接
     * @param map 键值对
     * @return
     * @throws IOException
     */
    private String post(String url, Map<String, String> map) throws IOException {

        FormBody.Builder build = new FormBody.Builder();
        for (Map.Entry<String, String> item : map.entrySet()) {
            build.add(item.getKey(), item.getValue());
        }
        RequestBody formBody = build.build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("POST-Key-Value");
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * post方法,使用json
     *
     * @param url 接口链接
     * @param json 请求体
     * @return
     * @throws IOException
     */
    private String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("POST-JSON");
            return response.body().string();
        } else {
            throw new IOException("错误码:" + response);
        }
    }

    /**
     * put方法,使用map传递键值对
     * @param url 接口链接
     * @param map 键值对
     * @return
     * @throws IOException
     */
    private String put(String url, Map<String, String> map) throws IOException {

        FormBody.Builder build = new FormBody.Builder();
        for (Map.Entry<String, String> item : map.entrySet()) {
            build.add(item.getKey(), item.getValue());
        }
        RequestBody formBody = build.build();

        Request request = new Request.Builder()
                .url(url)
                .put(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("PUT-Key-Value");
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * put方法,使用json
     * @param url 接口链接
     * @param json 键值对
     * @return
     * @throws IOException
     */
    private String put(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).put(body).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            System.out.println("PUT-JSON");
            return response.body().string();
        } else {
            throw new IOException("错误码:" + response);
        }
    }

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        Test test = new Test();
        String string = null;
//         try {
//             string = test.get("http://172.21.3.232:8088/operationLog");
//             System.out.println(string);
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//
//         try {
//             string = test.post("http://172.21.3.232:8088/operationLog?");
//             System.out.println(string);
//         } catch (IOException e) {
//             e.printStackTrace();
//         }

        try {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("operName", "android");
            map.put("loginName", "bug");
            map.put("ipAddress", "XXXXXXXXXXXXXXX");
            map.put("result", "sucess");
            map.put("description", "描述");
            map.put("failureReason", "失败原因");
            map.put("moduleId", "1");
            string = test.post("http://172.21.3.232:8088/operationLog?", map);
            //string = test.post("http://172.21.3.232:8088/operatorRole/addOperatorRole","{\"roleName\":\"aa\",\"roleDesc\":\"bb\",\"id\":\"\"}");
            System.out.println(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            string = test.put("http://172.21.3.232:8088/operatorRole/1","{\"id\": 1,\"roleDesc\": \"string\",\"roleName\": \"string\"}");
//            System.out.println(string);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值