2

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!

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
  • When you say multiple responses, what exactly does that imply? Are you getting two different json structures? Are you (for some reason) expecting two responses, one after the other, separated by some time interval? – farhanjk Apr 05 '19 at 22:26
  • @farhanjk - When I say multiple responses, as you said I am expecting 2 responses one after the other, separated by a specific time interval which is about 3 to 4 seconds. – SriSandhiya D Apr 05 '19 at 22:37
  • You may want to experiment with https://github.com/launchdarkly/okhttp-eventsource. – farhanjk Apr 05 '19 at 22:39
  • @farhanjk - I think github.com/launchdarkly/okhttp-eventsource is more of a messaging application. It would not work for my case:( – SriSandhiya D Apr 05 '19 at 22:57
  • Possible duplicate of [How do I do multiplexing on OkHttp?](https://stackoverflow.com/questions/49925071/how-do-i-do-multiplexing-on-okhttp) – Martin Zeitler Apr 06 '19 at 02:39
  • @MartinZeitler My case is a kind of server push i.e single request with two responses. – SriSandhiya D Apr 06 '19 at 04:55
  • @SriSandhiyaD this is called HTTP/2 multiplexing... your question does not even disclose, what these responses are. are you sure it isn't `WS` or `WSS` trying to upgrade the connection? – Martin Zeitler Apr 06 '19 at 04:57
  • @MartinZeitler It is a WSS request only. Added more details to my question. Appreciate your help!! – SriSandhiya D Apr 06 '19 at 05:06
  • @SriSandhiyaD if the protocol is `wss://`, you are probably using the wrong library. it's possible duplicate of: https://stackoverflow.com/questions/30547517/which-websocket-library-to-use-in-android-app hope this helps (as proper close-votes are meant to). – Martin Zeitler Apr 06 '19 at 11:07
  • @SriSandhiyaD using an `HTTP/2` client for `WSS` is non-sense; this has nothing to do with it. – Martin Zeitler Apr 08 '19 at 09:18
  • @SriSandhiyaD there only is a [feature request](https://github.com/square/okhttp/issues/4156) ... – Martin Zeitler Apr 08 '19 at 23:31

1 Answers1

1

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.

kissLife
  • 307
  • 1
  • 2
  • 9