【问题标题】:Can't handle UI after reconnecting to the server重新连接到服务器后无法处理 UI
【发布时间】:2014-04-03 08:14:05
【问题描述】:

我正在开发一个 Android 应用程序,由于收到响应的处理结果,我需要与服务器建立连接并更改 应用程序 UI,我正在使用 java 同步套接字编程 来处理那个连接和一个Handler 来处理UI 的变化。

每件事都在高效工作,但是当失去连接并再次重新连接时,输入被有效接收并通过处理得很好,任何依赖线程都运行完美,但是当涉及到与 UI 相关的任何事情时(即HandlerRunOnUiThread)它甚至没有进入它:

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();
        }
  }
}

这就是Login.Activity中的Handler:

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();
                }
              }
           }
        };

【问题讨论】:

    标签: java android handler


    【解决方案1】:

    我建议您通过触发 ConnectToServer 类的接口来更新 Activity 或 Fragment 中的 UI 元素。

    在您的登录活动中,实现 LoginInterface 并使用如下构造创建您的对象:

    new ConnectToServer(this);
    

    并创建一个界面

    public interface LoginInterface  {
        abstract void onLoginProcessFinished(String message);
    }
    

    在你的 ConnectToServer 类中,声明一个接口

    LoginInterface loginInterface;
    

    和你的构造函数

    public ConnectToServer(LoginInterface loginInterface) { this.loginInterface = 登录界面; }

    那么当你完成连接后,

    if(processing_ok){
    
       loginInterface.onLoginProcessFinished(response);
    }
    

    您的 connectToServer 将在您实现接口的登录活动中触发 onLoginProcessFinished,因此您将在更新 UI 的活动中拥有一个名为 onLoginProcessFinished 的方法。

    【讨论】:

    • 对不起,你能说得更具体点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2022-08-24
    • 2021-01-17
    • 2022-11-10
    相关资源
    最近更新 更多