重新连接到服务器后无法处理用户界面

编程入门 行业动态 更新时间:2024-10-28 13:28:54
本文介绍了重新连接到服务器后无法处理用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开发一个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.

更多推荐

重新连接到服务器后无法处理用户界面

本文发布于:2023-11-14 13:03:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   用户界面   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!