Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. Thread D= new Thread(new Runnable() {
  2.  
  3. @Override
  4. public void run() {
  5. SmackConfiguration.setDefaultPacketReplyTimeout(10000);
  6. XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  7. .setUsernameAndPassword("admin", "password")
  8. .setServiceName("192.168.0.200")
  9. .setHost("192.168.0.200")
  10. .setPort(5223).setSecurityMode(SecurityMode.ifpossible)
  11. .build();
  12.  
  13. AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
  14. try {
  15. conn2.connect();
  16.  
  17. conn2.login();
  18.  
  19. Presence presence = new Presence(Presence.Type.unavailable);
  20. presence.setStatus("Gone fishing");
  21. // Send the packet (assume we have an XMPPConnection instance called "con").
  22. conn2.sendStanza(presence);
  23.  
  24. } catch (SmackException | IOException | XMPPException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. Log.d("TAG",e.toString());
  28. }
  29.  
  30. ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
  31.  
  32.  
  33. Chat newChat = chatmanager.createChat("harsh@192.168.0.200");
  34.  
  35. try {
  36. newChat.sendMessage("Howdy!");
  37. }
  38. catch (NotConnectedException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42.  
  43.  
  44.  
  45. }
  46. });
  47.  
  48.  
  49.  
  50.  
  51. D.start();
  52.  
  53. public class NewClientActivity extends Activity {
  54. EditText etUsername, etPassword;
  55. Button bSubmit;
  56. AbstractXMPPConnection mConnection;
  57. ConnectionListener connectionListener = new ConnectionListener() {
  58. @Override
  59. public void connected(XMPPConnection xmppConnection) {
  60. Log.d("xmpp", "connected");
  61. try {
  62. SASLAuthentication.registerSASLMechanism(new SASLMechanism() {
  63. @Override
  64. protected void authenticateInternal(CallbackHandler callbackHandler) throws SmackException {
  65.  
  66. }
  67.  
  68. @Override
  69. protected byte[] getAuthenticationText() throws SmackException {
  70. byte[] authcid = toBytes('u0000' + this.authenticationId);
  71. byte[] passw = toBytes('u0000' + this.password);
  72. return ByteUtils.concact(authcid, passw);
  73. }
  74.  
  75. @Override
  76. public String getName() {
  77. return "PLAIN";
  78. }
  79.  
  80. @Override
  81. public int getPriority() {
  82. return 410;
  83. }
  84.  
  85. @Override
  86. public void checkIfSuccessfulOrThrow() throws SmackException {
  87.  
  88. }
  89.  
  90. @Override
  91. protected SASLMechanism newInstance() {
  92. return this;
  93. }
  94. });
  95. mConnection.login();
  96. } catch (XMPPException e) {
  97. runOnUiThread(new Runnable() {
  98. @Override
  99. public void run() {
  100. Toast.makeText(NewClientActivity.this, "Incorrect username or password", Toast.LENGTH_LONG).show();
  101. }
  102. });
  103. e.printStackTrace();
  104. } catch (SmackException e) {
  105. e.printStackTrace();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110.  
  111. @Override
  112. public void authenticated(XMPPConnection xmppConnection, boolean b) {
  113. Log.d("xmpp", "authenticated");
  114. runOnUiThread(new Runnable() {
  115. @Override
  116. public void run() {
  117. Toast.makeText(NewClientActivity.this,"Logged in successfully...",Toast.LENGTH_LONG ).show();
  118. }
  119. });
  120. }
  121.  
  122. @Override
  123. public void connectionClosed() {
  124. Log.d("xmpp", "connection closed");
  125. }
  126.  
  127. @Override
  128. public void connectionClosedOnError(Exception e) {
  129. Log.d("xmpp", "cononection closed on error");
  130. }
  131.  
  132. @Override
  133. public void reconnectionSuccessful() {
  134. Log.d("xmpp", "reconnection successful");
  135. }
  136.  
  137. @Override
  138. public void reconnectingIn(int i) {
  139. Log.d("xmpp", "reconnecting in " + i);
  140. }
  141.  
  142. @Override
  143. public void reconnectionFailed(Exception e) {
  144. Log.d("xmpp", "reconnection failed");
  145. }
  146. };
  147.  
  148. @Override
  149. protected void onCreate(Bundle savedInstanceState) {
  150. super.onCreate(savedInstanceState);
  151. setContentView(R.layout.activity_new_client);
  152. findViews();
  153. }
  154.  
  155. private void findViews() {
  156. etUsername = (EditText) findViewById(R.id.etUsername);
  157. etPassword = (EditText) findViewById(R.id.etPassword);
  158. bSubmit = (Button) findViewById(R.id.bSubmit);
  159. bSubmit.setOnClickListener(new View.OnClickListener() {
  160. @Override
  161. public void onClick(View v) {
  162. String[] params = new String[]{etUsername.getText().toString(), etPassword.getText().toString()};
  163. new Connect().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
  164. }
  165. });
  166. }
  167.  
  168. class Connect extends AsyncTask<String, Void, Void> {
  169. @Override
  170. protected Void doInBackground(String... params) {
  171.  
  172. XMPPTCPConnectionConfiguration config = null;
  173. XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
  174. builder.setServiceName("192.168.1.60").setHost("192.168.1.60")
  175. .setDebuggerEnabled(true)
  176. .setPort(5222)
  177. .setUsernameAndPassword(params[0], params[1])
  178. .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  179. .setCompressionEnabled(false);
  180. config = builder.build();
  181.  
  182. mConnection = new XMPPTCPConnection(config);
  183. try {
  184. //mConnection.setPacketReplyTimeout(10000);
  185. mConnection.addConnectionListener(connectionListener);
  186. mConnection.connect();
  187. } catch (SmackException e) {
  188. e.printStackTrace();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. } catch (XMPPException e) {
  192. e.printStackTrace();
  193. }
  194. return null;
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement