I have a Web API Asp .Net that can receive an HTTP Post request. I have tested the web service from Postman - Rest Client, or other similar application, and all works fine: the web service responses me immediately. But If I try to call the same web service from my Android application I have a problem:
The first call to web service is slow, while the nexts are fast. If I stop to call the ws, and then I retry, for example after 1 minute, the ws response is slow again, and the next ones are fast...an so on.
Here, the code I use for call web service.
String wsURI = "www.myWsUrl...";
url = new URL(wsURI);
try{
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoInput(true); // ?
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
JSONStringer requestData = new JSONStringer()
.object()
.key("id").value(12)
.key("frequence").value(1000)
.key("code").value("ABCAB-0123")
.endObject();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(httpConnection.getOutputStream()));
out.write(requestData.toString());
out.close();
resCode = httpConnection.getResponseCode();
...
}catch(){
...
}finally{
if(httpConnection!=null)
httpConnection.disconnect();
}