OkHttp3不返回响应

时间:2019-06-25 19:43:21

标签: android okhttp okhttp3

有很多使用OkHTTP3发出请求的示例,但是,我仍然无法发出正确的请求。据我所知,我的设置是正确的。

我一直在找回下面的回答。我不知道他们的意思。

  

{“ contentLength”:238,“ contentTypeString”:“ application / json”,“ source”:{“ buffer”:{“ size”:0},“ closed”:false,“ source”:{“ bytesRead “:0,”已完成“:false,”委托“:{”关闭“:false,”完成“: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字符串。我收到相同的结果。我究竟做错了什么?我是否必须将其包装在asynctask中?如果使用入队,我认为情况并非如此。

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:访问隐藏方法   Lcom / android / org / conscrypt / OpenSSLSocketImpl;-> getAlpnSelectedProtocol()[B   (浅灰色清单,反思)2019-06-25 12:46:27.698   23219-23248 / com.demo.android.address W / oid.address.dem:无法   解析Lokhttp3 / internal / http / RealResponseBody;注释类2132   2019-06-25 12:46:27.699 23219-23248 / com.demo.android.address   W / oid.address.dem:无法解析   Lokhttp3 / internal / http / RealResponseBody;注释类2132   2019-06-25 12:46:27.702 23219-23248 / com.demo.android.address   W / oid.address.dem:无法解析Lokhttp3 / ResponseBody;注解   班级2132 2019-06-25 12:46:27.703   23219-23248 / com.demo.android.address W / oid.address.dem:无法   解析Lokhttp3 / ResponseBody;注释类2132 2019-06-25   12:46:27.707 23219-23248 / com.demo.android.address W / oid.address.dem:   无法解析Lokio / Buffer;注释类2132 2019-06-25   12:46:27.708 23219-23248 / com.demo.android.address W / oid.address.dem:   无法解析Lokio / Buffer;注释类2132

1 个答案:

答案 0 :(得分:1)

您将转换为json而不是FROM。在这里应该是

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