OkHttp3 不返回响应

OkHttp3 Not Returning Back Response

OkHTTP3请求的例子有很多,但是我还是无法正确请求。据我所知,我的设置是正确的。

我不断收到如下所示的回复。不懂什么意思。

{"contentLength":238,"contentTypeString":"application/json","source":{"buffer":{"size":0},"closed":false,"source":{"bytesRead":0,"completed":false,"delegate":{"closed":false,"finished":true,"maxByteCount":16777216,"readBuffer":{"head":{"data":[123,34,105,115,85,115,101,114,83,117,98,115,99,114,105,98,101,100,34,58,116,114,117,101,44,34,115,101...]}

我发出请求的方式是传入一个请求对象,然后使用 RequestBody.create() 方法将我的请求对象转换为 OkHttp RequestBody 对象。我开始认为我的请求正文是错误的,所以我硬编码了一个 json 字符串。我收到相同的结果。我究竟做错了什么?我必须将其包装在异步任务中吗?如果使用排队,我认为情况并非如此。

private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public void createPostRequest(Request request, final ResultCallback callback,
                                  String url, HashMap<String, String> header) {
        // create gson object
        final Gson gson = new Gson();
        String testRequest = "{\"fName\": \"John\",\"lName\": \"Doe\",\"phoneNumber\": \"5555555\",\"isValidated\": \"false\",\"city\": \"Los Angeles",\"street\": \"Sesame Street\"}";

        // set header
        Headers headers = Headers.of(header);
        // form request body
        RequestBody requestBody = RequestBody.create(JSON, testRequest );
        // form request.
        Request okHttpRequest = new Request.Builder()
                .url(url)
                .post(requestBody)
                .headers(headers)
                .build();

        // prepares the request to be executed
        client.newCall(okHttpRequest).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                // handle error
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                if (response.isSuccessful()) {
                    String jsonString = gson.toJson(response.body());
                    // jsonString is gibberish as posted above
                }
            }
        });
    }

我的日志中没有收到任何错误。这就是从 OkHttp

返回的所有内容

2019-06-25 12:46:27.435 23219-23248/com.demo.android.address W/oid.address.dem: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (light greylist, reflection) 2019-06-25 12:46:27.698 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokhttp3/internal/http/RealResponseBody; annotation class 2132 2019-06-25 12:46:27.699 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokhttp3/internal/http/RealResponseBody; annotation class 2132 2019-06-25 12:46:27.702 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokhttp3/ResponseBody; annotation class 2132 2019-06-25 12:46:27.703 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokhttp3/ResponseBody; annotation class 2132 2019-06-25 12:46:27.707 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokio/Buffer; annotation class 2132 2019-06-25 12:46:27.708 23219-23248/com.demo.android.address W/oid.address.dem: Unable to resolve Lokio/Buffer; annotation class 2132

您转换为 json 而不是 FROM。应该是这样

String str = gson.fromJson("\"abc\"", String.class);