okhttp3 – Okhttp multiple response handling for single request android-ThrowExceptions

Exception or error:

I have an android application and I am trying to make an http/2 call with okHttp version 3.13.1, in my case for one request I will be receiving two responses sequentially from the server, but with the okHttp client I am getting only one response and if I try to send the request using curl command I am receiving two responses sequentially from the server.

Is there a way to handle multiple responses for a single http/2 request in android?

I tried various ways, but no luck:( Would be more than happy, if anyone could provide your inputs.

Thanks for your help and time in advance!!!

I am basically sending a get request from my android application using okHttp client to a server which supports http/2, so expecting to receive 2 different response for that single request in a timely manner.i.e the second response will be sent from the server after 5 seconds gap of receiving the first response.

Below is my response :

First response
event:initialize

{"session_id":"df313001-6461-431c-bcc1-7cb931bda4f5","deviceId":"YL0012345678"}

Second response
event:voice_response
data:{"intents":[{"version":0,"intent":"telstra_intent_voice","params":{"voiceResponse":{"displayText":"Launching now","vuiFileName":"E03.01.P2.V01.E.wav"},"action":{"ecp":"/launch-install/71361"}}}],"deviceId":"YL0012345678","channelId":"269671"}

But, in my case once I receive the first response the client is no longer listing to the server. When I receive the first response, in my log I see :
com.example.okhttp3 D/OkHttp: <– END HTTP (166-byte body),
so not receiving the second response.

Appreciate your help!

How to solve:

I found a workaround. You can send the response as multipart data. Then encapsulate all your responses into the multipart data seperated by ‘boundary’. The multipart data will not end until you send the ‘end mark'(0). Then the client side could decode all the responses one by one simultaneously when the server sending the multipart data.
The multipart data will looks like this:

--boundary
response_1

--boundary
response_2

...
--boundary--

0

there is also a demo here.

Leave a Reply

Your email address will not be published. Required fields are marked *