0

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) {

            }
        });
    }
Emre Alay
  • 1
  • 1

2 Answers2

1

The enqueue function performs the network access on a background thread. Therefore the result is not immediately available to you. To receive the result you must pass in an instance of Callback. When the request finishes in the other thread OkHttp will call the onResponse function of your callback instance. If the request failed it will call onFailure.

If you need the result in the calling thread and block until the result is available you can use the execute() method of the call class. Note that this will not work on the main thread in Android as network access is not allowed there.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • It is my exact problem I think. I have picked the image in main thread, so I have tried execute() in main, but had an exception as you said. How can I solve it? I am not familiar with the thread concept... – Emre Alay Oct 18 '19 at 10:48
  • It's quite difficult to give a good answer to this question because I don't know your code base. A simple approach would be to start some form of loading indicator, then call the `enqueue` function and when the `onResponse` or `onFailure` function is called you would stop the loading indicator and process/display the result. – Jannik Oct 18 '19 at 10:56
0

Retrofit 2: Get JSON from Response body

Go through above link if you want to post then just pass parameter to interface like mentioned follow

in api interface write

@FormUrlEncoded
@POST("append_url")
Call<CompanyDetailsResponse> getCompanyDetails(@Field("para") String para);

and use

Call<CompanyDetailsResponse> call = apiService.getCompanyDetails("para");

instead of

Call<CompanyDetailsResponse> call = apiService.getCompanyDetails();

and use model class according to your json response and drop comment if any query

Mahesh Pandit
  • 348
  • 3
  • 7