
Kali ini kita akan melanjutkan android socket client dengan menambahkan activity palindrome. Activity atau view ini akan digunakan untuk menangani fungsi polindrome yang bisa dihandle oleh server socket update.
Langsung saja, kita create Blank Activity lalu kita beri nama PalindromeActivity. Lalu akan terbentuk 2 file (java dan xml).
File “PalindromeActivity.java”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
public class PalindromeActivity extends ActionBarActivity { private Socket socket; private static final int SERVERPORT = 6789; private static final String SERVER_IP = "10.0.2.2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_palindrome); new Thread(new ClientThread()).start(); } @Override protected void onDestroy() { if (!socket.isClosed()) { try { DataOutputStream output = new DataOutputStream(socket.getOutputStream()); output.writeUTF("exit"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } super.onDestroy(); } public void onClick(View view) { if (socket.isClosed()) Toast.makeText(this.getBaseContext(), "Socket close restart aplikasi...", Toast.LENGTH_SHORT).show(); try { EditText et = (EditText) findViewById(R.id.editText01); String str = "#palindrome" + ":" + et.getText().toString(); DataOutputStream output = new DataOutputStream(socket.getOutputStream()); output.writeUTF(str); new ReceiveMessageFromServer().execute(""); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } class ReceiveMessageFromServer extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { DataInputStream input; String response = ""; try { input = new DataInputStream(socket.getInputStream()); response = input.readUTF(); } catch (IOException e) { e.printStackTrace(); } return response; } @Override protected void onPostExecute(String result) { TextView txt = (TextView) findViewById(R.id.textView1); txt.setText(result); if (result.equalsIgnoreCase("bye")) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(Void... values) { } } class ClientThread implements Runnable { @Override public void run() { try { InetAddress serverAddr = InetAddress.getByName(SERVER_IP); socket = new Socket(serverAddr, SERVERPORT); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } } } |
file “activity_palindrome.xml” yang berada di res/layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" > </EditText> <Button android:id="@+id/myButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="Send" > </Button> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout> |
Rubah file “AndroidManifest.xml” menjadi:
1 2 3 4 5 6 7 8 9 |
<activity android:name=".PalindromeActivity" android:label="@string/title_activity_palindrome" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> |