Autobahn An application demonstration and global of Android

Source: Internet
Author: User

First introduce the next Autobahn Android is what thing, in short, is GitHub on an open source project, the purpose is to implement the WebSocket protocol on the Android side, the project address here, read the source of children's shoes can go to see, address point here, seems to have some release/ Subscribe to the pattern of something inside.

Next, I want to achieve the goal, first I want to establish a global continuous connection, in order to ensure that in each activity, can call its method of sending data

WebSocketUitls.webSocketConnection.sendTextMessage (Jsonobject.tostring ());

The other point is that, after sending the data, you can execute the appropriate method in the current context when you receive the returned data.

@Overridepublic void Mysystemmethod (Jsonobject json) {try {System.out.println (json.get ("data") + "_loginpage");        UserName = (EditText) Findviewbyid (r.id.username);    Username.settext (Json.get ("Data"). ToString ());    } catch (Jsonexception e) {e.printstacktrace (); }}

------------------------------------Split Line------------------------------------------------

First, I define a tool class that websocket the connection object and define a pseudo-processing class that inherits the Mywebsockethandler interface, and the public void ontextmessage that returns the data in the global (String Payload), so that when other activity classes, new Mywebsockethandler, you can replace the methods in Ontextmessage to achieve the effect of the callback

public class websocketuitls{    public static string ws =   "WS://IP Address: Port number";    public static websocketconnection  websocketconnection;    public static websockethandler websockethandler;     static {        webSocketConnection  = new websocketconnection ();         websockethandler =  new websockethandler () {            // WebSocket ways to accept return data              @Override              public void ontextmessage (String  payload)  {                 super.ontextmessage (payload);                  // Call the Mysystemmethod in the custom method and complete the replacement when other activity resets him                  JSONObject jsonObject;                 try {                     jsonObject = new  Jsonobject (payload);                     //to replace the method                      myhandler.mysystemmethod (Jsonobject);                 } catch  (jsonexception  e)  {                    e.printstacktrace ();                 }             }        } ;        try {             websocketconnection.connect (Ws,websockethandler);         } catch  (websocketexception e)  {             e.printstacktrace ();        }     }    //defines an initial global class     public static   Mywebsockethandler myhandler  = new mywebsockethandler () {           @Override         public void mysystemmethod (JSONObject  json)  {            //TODO  no eggs. This method only occupies a position to facilitate later replacement of         }    };}
public class loginpage extends appcompatactivity{    private  edittext username;     @Override     protected void  OnCreate (bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         setcontentview (R.layout.activity_login_ page),         //the new method to myhandler the global approach          websocketuitls.myhandler = new loginhandler ();         Button login_btn =  (Button) Findviewbyid (r.id.login_in);         login_btn.setonclicklistener (New view.onclicklistener ()  {              @Override             &nBsp;public void onclick (VIEW&NBSP;V)  {                 final Map<String,String> map = new  Hashmap<string,string> ();                 map.put ("type", "xxxxxxxxx");                 map.put ("name", "111111");                 map.put ("Password",  "111111");                 final jsonobject jsonobject  = new jsonobject (map);                 //combined data, this is the request parameter of WebSocket                &nbsP; websocketuitls.websocketconnection.sendtextmessage (Jsonobject.tostring ());             }        });     }    //implements an internal class that implements the Mywebsockethandler interface at the same time,     class  LoginHandler implements MyWebSocketHandler{          //implement this method to replace the global processing method          @Override          public void mysystemmethod (Jsonobject json) {             try {                 system.out.println (Json.get ("Data")  +  "_loginpage");                 userName  =  (EditText) Findviewbyid (r.iD.username);                 Username.settext (Json.get ("Data"). ToString ());             } catch  (jsonexception e)  {                 e.printstacktrace ();             }        }    }}
Public interface Mywebsockethandler {public void Mysystemmethod (Jsonobject json);}

--------------------------------------Split Line----------------------------------------------

May say is not very organized, after all, limited capacity, there is a great God used this thing, have their own ideas can be put forward in the message.

Also attached is a autobahn-0.5.0.jar bag. There is a need can be used in their own projects, only one, other online download things, there may be two dependencies, in fact, and, do not need to add in will compile errors, so only to join this also can be.


This article is from the "Do Not Laugh" blog, please make sure to keep this source http://8779947.blog.51cto.com/8769947/1734381

Autobahn An application demonstration and global of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.