Android client to send message to Java server on Raspberry Pi

Its a Java client and server exercise in my another blogspot HelloraspberryPi. The client connect to server and send something, the server simple print the received message and close.
Its Android version of the client, to connect to server and send something.
MainActivity.java
package com.blogspot.android_er.androidclient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
EditText editTextAddress, editTextPort, editTextMsg;
Button buttonConnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = (EditText) findViewById(R.id.address);
editTextPort = (EditText) findViewById(R.id.port);
editTextMsg = (EditText) findViewById(R.id.msgtosend);
buttonConnect = (Button) findViewById(R.id.connect);
buttonConnect.setOnClickListener(buttonConnectOnClickListener);
}
View.OnClickListener buttonConnectOnClickListener =
new View.OnClickListener() {
@Override
public void onClick(View arg0) {
MyClientTask myClientTask = new MyClientTask(
editTextAddress.getText().toString(),
Integer.parseInt(editTextPort.getText().toString()));
myClientTask.execute(editTextMsg.getText().toString());
}
};
public class MyClientTask extends AsyncTask<String, Void, Void> {
String dstAddress;
int dstPort;
String response;
MyClientTask(String addr, int port) {
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(String... params) {
try {
Socket socket = new Socket(dstAddress, dstPort);
OutputStream outputStream = socket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(params[0]);
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidclient.MainActivity">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />
<EditText
android_id="@+id/address"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_hint="dstAddress" />
<EditText
android_id="@+id/port"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_hint="dstPort" />
<EditText
android_id="@+id/msgtosend"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_hint="msg to send..." />
<Button
android_id="@+id/connect"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Connect and send..."/>
</LinearLayout>
uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml
The Java code of the server side, refer to "Java Network exercise: client and server - client send something to server".
The next example "Android client example 2, communicate with Java Server run on Raspberry Pi" show how Android Client connect to Java Server (run on raspberry Pi) and keep connected, and send to and receive from Server.
alternative link download