I’m writing a simple client-server system and the question is: how to structure my client code in order to get POST request-response working in a loop?
At the moment it looks something like this (and it’s is NOT a loop right now):
- open HttpURLConnection
- set properties
- setDoOutput(true)
- writing to output stream
- closing output stream
- new DataInputStream
- reading response
- exiting method
I’m not sure which objects do I have to save for the next iterations and which ones I should close.
you need to save the connection object and you should make use of
setDoInput(true)for reading data but if you just want to readresponseCodeandresponseMessageyou dont needInputStream. check the code below.for
InputStreamReader