I am currently developing socket communication between android and server, which is a simple java program run in terminal. Things are going well except there is a warning always appears in the logCat when I close the application:
IInputConnectionWrapper showStatusIcon on inactive InputConnection
I am searching on the internet to find out the problem I found a post in StackOverflow Similar Problem. The difference is that I can send and receive information well in my program. The answer to this similar problem is that connection is not closed. Does that mean I didn’t call socket.close(); after operation? That leads to a more complicated problem of implementation.
First of all, I want a single, static socket to listen and send to a server. Because I might not close the socket every time I transmit something, so I just close it after the listener finishes the work.
Detail codes are posted below
I initialize connection in the constructor as:
client = new Socket(mServerName, mport);
out = new DataOutputStream(client.getOutputStream());
inFromServer = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(inFromServer);
And let them be there during the whole process.
I wrote transmitting from android to server into a function as following:
public void sendRequest(int type, int what1, int what2, Object data)
{
try{
if(client.isConnected())
{
out.writeUTF(encoded(type,what1,what2,data) + "\n");
}
}catch(IOException e){
Log.e(TAG, "IOException at SendRequest");
e.printStackTrace();
}
}
Listener in a new thread:
try {
String line = null;
while ((line = reader.readLine()) != null)
{
ReceiveHandler(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException at StartListen");
e.printStackTrace();
}finally{
try {
// The Only Place that I close the Socket
inFromServer.close();
out.close();
reader.close();
client.close();
} catch (IOException e) {
Log.e(TAG, "Close Socket with IOException " + e.toString());
e.printStackTrace();
}
}
My Question is:
Is there something wrong with my implementation or are there better ways to do this?
Thanks so much for your help!
There is a tool that can help you diagnose network related issues both on Android devices and in the emulator. It may help you to track down the issue a bit further. The ARO tool is open source and available here http://developer.att.com/developer/forward.jsp?passedItemId=9700312