First delay in web API call from Android app
I have an Asp.Net Web API that can receive an HTTP request. I tested a web service from Postman - Rest Client or some other similar application and everything works great: the web service responds instantly to me. But if I try to call the same web service from my Android app I have a problem:
The first call to the web service is slow and the next is fast. If I stop calling ws and then repeat, for example after 1 minute, the ws response is slow again, and the next ones are fast ... and so on.
Here's the code I am using for the web service call.
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();
}
+3
source to share
No one has answered this question yet
Check out similar questions: