I am a beginner in android. I do have some experience with java, but can not solve this problem for some time.
Here is some background information: I am trying to make a mobile application, which picks an image from the gallery or camera then send it to my server. I am done with picking the image, converting it to base64, but somehow I can not complete the function for http post request. I want this function to return the response from the server, but I am failing.
I have highlighted a part of the function with a comment. The part I do not understand is Overridden methods. As I said, I am a beginner and Android Studio made me include these methods(OnResponse, OnFailure), with these methods I can not return the response because they are void; and without these methods I am having an exception which I have no clue where it is coming from.
public void postRequest(String postdata) throws IOException {
MediaType MEDIA_TYPE = MediaType.parse("application/json");
String url = "https://ptsv2.com/t/z497u-xxx/post";
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MEDIA_TYPE, postdata);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//THIS IS THE PART I DO NOT UNDERSTAND
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
message = response.body().string();
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
});
}