【问题标题】:POST json okhttpPOST json okhttp
【发布时间】:2017-09-29 17:13:47
【问题描述】:

我有一个简单的测试,我正在测试 json 的 POST 是否成功。这是我的测试:

int expectedCodeResponse = 201;
Map<String, String> params = new HashMap<String, String>();
    params.put("batchPublicId", "001");
    params.put("fileSize", "2");
    params.put("location", "{\\\"bucket\\\":\\\"blabla\\\"}");
    params.put("title", "testInvoice");
    JSONObject parameter = new JSONObject(params);

OkHttpClient httpClient = new OkHttpClient();

    RequestBody body = RequestBody.create(JSON, parameter.toString());
    Request request = new Request.Builder()
            .url(environmentUrls.get("base_url") + "/invoices")
            .post(body)
            .addHeader("content-type", "application/json; charset=utf-8")
            .build();


    Response response = httpClient.newCall(request).execute();

    Assert.assertEquals(response.code(), expectedCodeResponse);

现在它不再起作用了,因为 fileSize 不再是 String,而是 int。我该如何解决这个问题?

【问题讨论】:

标签: json post okhttp3


【解决方案1】:

将您的字符串转换为 int like this,并评估两个 int 的相等性。

    ...

    Response response = httpClient.newCall(request).execute();

    Assert.assertEquals(Integer.parseInt(response.code()), expectedCodeResponse);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 2016-03-06
    • 2017-11-11
    • 2017-03-24
    • 2015-12-20
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多