重新连接到服务器后无法处理用户界面 [英] Can't handle UI after reconnecting to the server

查看:210
本文介绍了重新连接到服务器后无法处理用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,需要我建立与服务器的连接并更改应用UI 由于收到答复的处理结果,我使用的的Java同步socket编程的来处理连接和处理程序来处理 UI 的变化。

每一件事情是工作效率,但松动的连接,然后再次重新连接时,输入有效的接收和处理得很好处理任何根据线程运行完美,但未来相关的任何事情时,在 UI (即处理程序 RunOnUiThread )。它甚至没有进入它:

 公共类ConnectToServer实现Runnable {公共静态Socket套接字;
私人的BufferedReader输入;
公共静态DataOutputStream类输出;公共无效连接(){
        尝试{
            插座=新的Socket(地址,端口);
        }赶上(例外五){
            e.printStackTrace();
        }
    }公共字符串getServerRespons(登录登录)抛出JSONException {//此处登录是Android的活动,我会的方法来处理用户界面的变化的一个实例InputStream的流= NULL;
        尝试{
            流= socket.getInputStream();
        }赶上(例外五){
            e.printStackTrace();
        }        如果(流!= NULL){
            输入=新的BufferedReader(新的InputStreamReader(
                    流));        尝试{
            尺蠖prepare()。
            而(真)
                    {
                responseLine = input.readLine();
                server_response = server_response + responseLine;
                  //
                  //一些处理服务器响应
                  //
             如果(processing_ok){                 ((登录)ConnectToServer.login)的.sys(); //这是登录活动的方法,它包含运行非常有规律线程
                尝试{
                    ((登录)ConnectToServer.login).updateUI.obtainMessage(5,0,0,响应).sendToTarget();
                        }
                     赶上(例外五){
                            e.printStackTrace(); //没有逮住
                        }
              的System.out.println(打印); //打印也没有问题               }
        }赶上(IOException异常五){
            e.printStackTrace();
        }
  }
}

这是处理程序的 Login.Activity

 公众最终处理程序的updateUI =新的处理程序(){
           @覆盖
           公共无效的handleMessage(消息MSG)
           {
              如果(msg.what == 5){
                  的System.out.println(这里); //即使不重新连接后打印
                  尝试{
                    response_received((字符串)msg.obj);
                }赶上(例外五){
                    e.printStackTrace();
                }
              }
           }
        };


解决方案

我建议你从你的ConnectToServer类引发的界面更新您的活动或片段的UI元素。

在您登录活动,实施LoginInterface和这样的结构创建对象:

 新ConnectToServer(本);

和创建一个接口

 公共接口LoginInterface {
    抽象无效onLoginProcessFinished(字符串消息);
}

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

  LoginInterface loginInterface;

和你的构造

公共ConnectToServer(LoginInterface loginInterface){
    this.loginInterface = loginInterface;
}

然后当你与你的连接完成后,

 如果(processing_ok){   loginInterface.onLoginProcessFinished(响应);
}

您connectToServer将触发您的登录活动onLoginProcessFinished,你实现的接口,这样你才会有一个名为onLoginProcessFinished您的活动方法,其中您更新UI。

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

解决方案

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.

这篇关于重新连接到服务器后无法处理用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆