Guest User

Untitled

a guest
Sep 18th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. package com.spendingtracker.readdeo.spendingtracker;
  2.  
  3. import android.content.Intent;
  4. import android.os.Handler;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.BufferedWriter;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.io.ObjectOutputStream;
  18. import java.io.OutputStream;
  19. import java.io.OutputStreamWriter;
  20. import java.io.PrintWriter;
  21. import java.net.InetAddress;
  22. import java.net.ServerSocket;
  23. import java.net.Socket;
  24. import java.net.UnknownHostException;
  25. import java.util.UUID;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28. @Override
  29. protected void onResume() {
  30. super.onResume();
  31.  
  32. new Thread(new ClientThread()).start();
  33.  
  34. updateConversationHandler = new Handler();
  35.  
  36. this.serverThread = new Thread(new ServerThread());
  37. this.serverThread.start();
  38. }
  39.  
  40. @Override
  41. protected void onPause() {
  42. super.onPause();
  43. /*
  44. try {
  45. serverSocket.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. try {
  50. socketC.close();
  51.  
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. */
  56. }
  57.  
  58. private ServerSocket serverSocket;
  59. Handler updateConversationHandler;
  60. Thread serverThread = null;
  61. private static final int CLIENTPORT = 5000;
  62. Button button3, button4;
  63. TextView text;
  64.  
  65. private Socket socketC;
  66.  
  67. private static final int SERVERPORT = 8000;
  68. private static final String SERVER_IP = "195.228.24.3";
  69.  
  70. TextView textview7, textview2, textview3, textview4, textview5, textview6;
  71. Button button1, button2;
  72. EditText et1, et2, et3, et4, et5, et6;
  73.  
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.activity_main);
  78. textview7 = (TextView) findViewById(R.id.textView7);
  79.  
  80. Button button1 = (Button) findViewById(R.id.button);
  81. Button button2 = (Button) findViewById(R.id.button2);
  82.  
  83. // new Thread(new ClientThread()).start();
  84.  
  85. updateConversationHandler = new Handler();
  86.  
  87. this.serverThread = new Thread(new ServerThread());
  88. this.serverThread.start();
  89.  
  90. button2.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. startActivity(new Intent(MainActivity.this, Main2Activity.class));
  94. /*
  95. try {
  96. serverSocket.close();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. try {
  101. socketC.close();
  102.  
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. */
  107. }
  108. });
  109.  
  110. button1.setOnClickListener(new View.OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. try {
  114. // new Thread(new ClientThread()).start();
  115. String uniqueID = UUID.randomUUID().toString();
  116. long unixTime = System.currentTimeMillis() / 1000L;
  117.  
  118. EditText et1 = (EditText) findViewById(R.id.editText);
  119. EditText et2 = (EditText) findViewById(R.id.editText2);
  120. EditText et3 = (EditText) findViewById(R.id.editText3);
  121. EditText et4 = (EditText) findViewById(R.id.editText4);
  122. EditText et5 = (EditText) findViewById(R.id.editText5);
  123. EditText et6 = (EditText) findViewById(R.id.editText6);
  124. ///
  125. String str1 = et1.getText().toString();
  126. String str2 = et2.getText().toString();
  127. String str3 = et3.getText().toString();
  128. String str4 = et4.getText().toString();
  129. String str5 = et5.getText().toString();
  130. String str6 = et6.getText().toString();
  131.  
  132. if (str1 != null && !str1.isEmpty());{str1 = "0";};
  133. if (str2 != null && !str2.isEmpty());{str2 = "0";};
  134. if (str3 != null && !str3.isEmpty());{str3 = "0";};
  135. if (str4 != null && !str4.isEmpty());{str4 = "0";};
  136. if (str5 != null && !str5.isEmpty());{str5 = "0";};
  137. if (str6 != null && !str6.isEmpty());{str6 = "0";};
  138.  
  139. String Msg = "WriteNewSpend,Readdeo,"+unixTime+","+str1+","+str2+","
  140. +str3+","+str4+","+str5+","+str6+","+uniqueID;
  141. PrintWriter out = null;
  142. try {
  143. out = new PrintWriter(new BufferedWriter(
  144. new OutputStreamWriter(socketC.getOutputStream())),
  145. true);
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149. out.println(Msg);
  150. }
  151.  
  152. finally {
  153. /*
  154. try {
  155. socketC.close();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. */
  160. // new Thread(new ClientThread()).start();
  161. }
  162.  
  163.  
  164. }
  165. });
  166. }
  167. class ClientThread implements Runnable {
  168. @Override
  169. public void run() {
  170. try {
  171. // InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  172. socketC = new Socket("192.168.1.110", 5000);
  173. } catch (UnknownHostException e1) {
  174. e1.printStackTrace();
  175. } catch (IOException e1) {
  176. e1.printStackTrace();
  177.  
  178. }
  179.  
  180. }
  181. }
  182. /*
  183. public void sendMessageToServer(String message) {
  184.  
  185. if (message != null) {
  186. try {
  187. //Assume I already have an instance of client Socket:
  188. //Socket outgoingConn = new Socket(host, port)
  189. //1. I get the OutputStream of my outgoing connection
  190. OutputStream outStream = socket.getOutputStream();
  191. //2. Create an object output stream
  192. ObjectOutputStream objectWriter = new ObjectOutputStream(outStream);
  193. //3. Write the object
  194. objectWriter.writeObject(message);
  195. //Close the io if required (would recommend try-with-resources if using jdk7)
  196. } catch (IOException e) {
  197. // TODO Auto-generated catch block
  198. e.printStackTrace();
  199. }
  200. }
  201.  
  202. }
  203. */
  204. class ServerThread implements Runnable {
  205. public void run() {
  206. Socket socket = null;
  207. try {
  208. InetAddress srvAddr = InetAddress.getByName("0.0.0.0");
  209. serverSocket = new ServerSocket(SERVERPORT);
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. }
  213. while (!Thread.currentThread().isInterrupted()) {
  214. try {
  215. socket = serverSocket.accept();
  216. CommunicationThread commThread = new CommunicationThread(socket);
  217. new Thread(commThread).start();
  218. } catch (IOException e) {
  219. e.printStackTrace();
  220. }
  221. }
  222. }
  223. }
  224. class CommunicationThread implements Runnable {
  225.  
  226. private Socket clientSocket;
  227.  
  228. private BufferedReader input;
  229.  
  230. public CommunicationThread(Socket clientSocket) {
  231.  
  232. this.clientSocket = clientSocket;
  233.  
  234. try {
  235.  
  236. this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
  237.  
  238. } catch (IOException e) {
  239. e.printStackTrace();
  240. }
  241. }
  242.  
  243. public void run() {
  244.  
  245. while (!Thread.currentThread().isInterrupted()) {
  246.  
  247. try {
  248.  
  249. String read = input.readLine();
  250. if(read != null)
  251. {
  252. updateConversationHandler.post(new updateUIThread(read));
  253. }
  254. } catch (IOException e) {
  255. e.printStackTrace();
  256. }
  257. }
  258. }
  259.  
  260. }
  261. class updateUIThread implements Runnable {
  262. private String msg;
  263.  
  264. public updateUIThread(String str) {
  265. this.msg = str;
  266. }
  267.  
  268. @Override
  269. public void run() {
  270. textview7.setText("\r\n"+"Client Says: "+ msg + textview7.getText().toString()+ "\r\n"
  271. );
  272. }
  273. }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment