0

I want to a upload file on my server and I've decided to try OKHTTP instead of my current method which is based on android own HTTP implementation and AsyncTask.

Anyway, I used OKHTTP and its asynchronous implementation (from its own recipes) but it returns an empty message (the request code is ok, the message is empty) in both GET and POST methods. Did I implement it wrong or is there anything else remained that I did not considered? In the meantime, I couldn't find a similar case except this which says used AsyncTask.

Here's the code:

Request request;
Response response;
private final OkHttpClient client = new OkHttpClient();
private static final String postman_url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2";
String message_body;

public void Get_Synchronous() throws IOException
{
    request = new Request.Builder()
            .url(postman_url)
            .build();

    Call call = client.newCall(request);
    response = call.execute();

    message_body = response.toString();
    //assertThat(response.code(), equalTo(200));
}

public void Get_Asynchronous()
{
    request = new Request.Builder()
            .url(postman_url)
            .build();

    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        public void onResponse(Call call, Response response)
                throws IOException
        {
            message_body = response.toString();
            
        }

        public void onFailure(Call call, IOException e)
        {
            
        }
    });
}

Edit: I catch the log on response:

onResponse: Response{protocol=h2, code=200, message=, url=https://postman-echo.com/get?foo1=bar1&foo2=bar2}

ALirezaSO
  • 49
  • 11
  • Can you please add the log response? Also, can you log the failure iOException? It will help understand the error better. What is the expected response? Many times file upload apis just return 200 to confirm that file is uploaded and nothing else. – Mohit Ajwani May 17 '21 at 05:40
  • Thanks, @MohitAjwani I've added the log – ALirezaSO May 17 '21 at 05:52
  • So this API is returning empty message. Although it is success. So, this is the standard API behavior. – Mohit Ajwani May 17 '21 at 06:08
  • Dear @MohitAjwani It should return something like this: " {"args":{"foo1":"bar1","foo2":"bar2"},"headers":{"x-forwarded-proto":"https","x- ..." – ALirezaSO May 17 '21 at 06:17
  • Try adding the converter factory - https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter This should help to convert the response to an object so you can print it. – Mohit Ajwani May 17 '21 at 06:25
  • @MohitAjwani does it work for the okhttp? I think it's just for Retrofit – ALirezaSO May 17 '21 at 06:34
  • Yes my bad, it works with Retrofit. You need a converter to object like this. Gson gson = new Gson(); ResponseBody responseBody = client.newCall(request).execute().body(); SimpleEntity entity = gson.fromJson(responseBody.string(), SimpleEntity.class); // log the entity – Mohit Ajwani May 17 '21 at 07:33

1 Answers1

1

OK, for anyone who wants to receive an string from a call, response and response.messgage() don't provide that. To catch the response from your provider, you just need to call response.body().string() inside onResponse which returns the message inside your request.

But after all, Retrofit is a better choice if you want to receive a JSON file using .addConverterFactory(GsonConverterFactory.create(gson)).

If you still want to receive an string just use .addConverterFactory(ScalarsConverterFactory.create()) as explained here.

ALirezaSO
  • 49
  • 11