首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Okhttp3 -如何在Okhttp3客户端中定义特定请求

Okhttp3 -如何在Okhttp3客户端中定义特定请求
EN

Stack Overflow用户
提问于 2021-08-26 16:13:35
回答 1查看 174关注 0票数 1

我有一个递归方法,它使用okhttp3向端点发出连续的分页请求,直到属性不再存在为止。我的测试运行模拟了过程中的最后一个调用,但是我在进行2步迭代时遇到了困难。

如何在newCall()方法中定义特定的ohkttp3.Request,使其不会每次抛出一个NullPointer?下面是我的代码/测试以供参考。

错误: java.lang.NullPointerException

递推法

代码语言:javascript
复制
public JSONArray recursiveMethod(JSONArray array, String authCode, String requestUrl) {
        String nextPageAtt = "newPage";
        String newPage = null;
        Request request = new Request.Builder()
                .url(requestUrl)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authCode)
                .build();
                
        try {
            //Problem child
            okhttp3.Response response = client.newCall(request).execute();
            if (response.code() == SUCCESS_CODE && response.body() != null) {

                //Add JSON data to array 
                
                //if "newPage" attribute in response, set it here
                //only present when page != last page
                
            } else {
            }
        }
        //"newPage" attribute won't be present on the last page of data"
        if (newPage != null) {
            recursiveMethod(array, authCode, baseUrl + newPage);
        }
        return array;
    }

测试方法

代码语言:javascript
复制
@Test
void recursiveMethodNonewPage() throws IOException {
        JSONArray emptyArray = new JSONArray();
    
        final Response successResponse = new Response.Builder()
                .request(new Request.Builder().url("https://localhost:8080/getData").build()) //"newPage" not there (last call)
                .protocol(Protocol.HTTP_1_1)
                .code(SUCCESS_CODE).message("").body(
                        ResponseBody.create(
                                MediaType.parse("application/json"),
                                "/////JSON/////"
                        ))
                .build();
                
        when(remoteCall.execute()).thenReturn(successResponse);
        
        //this works when I pass "any()" but a java.lang.NullPointerException is thrown when I enter a specific Request
        when(mockClient.newCall(any())).thenReturn(remoteCall);
        
        //******Issue******
        //Need to pass a specific request on in order to simulate multiple recursive iterations
        /*Request request = new Request.Builder()
                .url(https://localhost:8080/getData)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authCode)
                .build();*/
                
        //Throws Null
        //when(mockClient.newCall(request))).thenReturn(remoteCall);
        
        JSONArray newArray = getDataUtil.recursiveMethod(emptyArray, authCodeSuccess, "https://localhost:8080/getData");
        assertEquals(4, newArray.length());
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-26 19:14:30

终于在这里找到了答案:https://stackoverflow.com/a/8395685/2573297

代码语言:javascript
复制
when( method-call ).thenReturn( value1, value2, value3 );

工作测试

代码语言:javascript
复制
@Test
void recursiveMethodNonewPage() throws IOException {
        JSONArray emptyArray = new JSONArray();
    
        final Response pageOne = new Response.Builder()
                .request(new Request.Builder().url("https://localhost:8080/getData").build())
                .protocol(Protocol.HTTP_1_1)
                .code(SUCCESS_CODE).message("").body(
                        ResponseBody.create(
                                MediaType.parse("application/json"),
                                "/////JSON/////"
                        ))
                .build();
                
        final Response pageTwo = new Response.Builder()
                .request(new Request.Builder().url("https://localhost:8080/getData").build())
                .protocol(Protocol.HTTP_1_1)
                .code(SUCCESS_CODE).message("").body(
                        ResponseBody.create(
                                MediaType.parse("application/json"),
                                "/////JSON/////"
                        ))
                .build();
        
        when(mockClient.newCall(any())).thenReturn(remoteCall);
        when(remoteCall.execute()).thenReturn(pageOne, pageTwo);
        
        JSONArray newArray = getDataUtil.recursiveMethod(emptyArray, authCodeSuccess, "https://localhost:8080/getData");
        assertEquals(4, newArray.length());
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68942037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档