问题
I'm developing an Android app that requires me to establish a connection with the server and changing the App UI due to processing results of the received responses, I'm using java synchronous socket programming to handle that connection and a Handler to handler UI changes.
Every thing is working efficiently but when loosing the connection and reconnect again, The Input received efficiently and handled by processing very well and any depending threads running perfectly but when coming to any thing related to the UI (ie. Handler, RunOnUiThread) it doesn't even goes into it:
public class ConnectToServer implements Runnable {
public static Socket socket;
private BufferedReader input;
public static DataOutputStream output;
public void connect() {
try {
socket = new Socket(address, port);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getServerRespons(Login login) throws JSONException { // login here is an instance of the Android Activity that i will it's methods to handle the UI changes
InputStream stream = null;
try{
stream = socket.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
if(stream != null){
input = new BufferedReader(new InputStreamReader(
stream));
try {
Looper.prepare();
while (true)
{
responseLine = input.readLine();
server_response = server_response + responseLine;
//
// some processing to the server response
//
if(processing_ok){
((Login)ConnectToServer.login).sys();// this is a method in Login-Activity and it contains a regular thread that runs perfectly
try{
((Login)ConnectToServer.login).updateUI.obtainMessage(5, 0, 0, response).sendToTarget();
}
catch(Exception e){
e.printStackTrace();// nothing catched
}
System.out.println("print");//also printed without problems
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
And that is the Handler in the Login.Activity:
public final Handler updateUI = new Handler(){
@Override
public void handleMessage(Message msg)
{
if(msg.what == 5){
System.out.println("here");//even that doesn't printed after reconnect
try {
response_received((String)msg.obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
回答1:
I would recommend you update your UI elements in your activity or fragment with triggering an interface from your ConnectToServer class.
In your Login activity, implement LoginInterface and create your object with a construct like this:
new ConnectToServer(this);
and create an interface
public interface LoginInterface {
abstract void onLoginProcessFinished(String message);
}
in your ConnectToServer class, declare an interface
LoginInterface loginInterface;
and your constructor
public ConnectToServer(LoginInterface loginInterface) { this.loginInterface = loginInterface; }
then when you are done with your connection,
if(processing_ok){
loginInterface.onLoginProcessFinished(response);
}
your connectToServer will trigger the onLoginProcessFinished in your Login activity where you implemented the interface, so you will have a method called onLoginProcessFinished in your activity where you update your UI.
来源:https://stackoverflow.com/questions/22831612/cant-handle-ui-after-reconnecting-to-the-server