Android 3.0 socket response latency
I have started an Android project utilizing socket programming. Platform is Android 3.0. Due to platform requiremen开发者_如何转开发ts I am applying the socket operations in AsyncTask.
I connect to the server, send commands successfully but the problem is that the responses I receive come incomplete. After sending a command, I have to wait for sometime for the response to come completely, otherwise it is just partial response. The way I found out I get the full response when I wait is during debug mode, I waited a while before going on to the next line of code which I utilize the response variable and I got the full expected result.
I tried putting a timer
Timer myTimer = new Timer();
myTimer.wait(2000);
but that just made the whole response not work at all. Any suggestions would be much appreciated.
First of all, you shouldn't be having this problem. There is probably something wrong with your network connection, because that amount of latency is not normal for any platform. But if you do want to tackle it programmatically, read on.
You don't want to use a timer for that, because:
a) even after waiting you can't be certain you've got the value, and
b) you will be slowing things down unnecessarily if you do get the value quickly.
The better option is to have some code that checks the received value in a loop to see if it's complete, and once it is, move on to the next bit.
精彩评论