Reputation: 7856
There are a lot of examples for making requests with OkHTTP3, however, I am still unable to make a proper request. As far as I know, my setup is correct.
I keep getting back responses that look like below. I do not know what they mean.
{"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...]}
The way I am making my requests is that I am passing in a request object, and then I use the RequestBody.create() method to convert my request object into the OkHttp RequestBody object. I started thinking that my request body is wrong so I hardcoded a json string. I am receiving the same results. What am I doing wrong? Do I have to wrap this in asynctask? I did not think this was the case if using enqueue.
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
}
}
});
}
I am not receiving any errors in my logs. This is all that comes back from 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
Upvotes: 0
Views: 895
Reputation: 2607
you convert TO json instead of FROM. Here how it suppose to be
String str = gson.fromJson("\"abc\"", String.class);
Upvotes: 1