Android Customer Illustration 2, Communicate Amongst Coffee Server Operate On Raspberry Pi

Posted on

Refer to the previous Android customer example to post message to Java server on Raspberry Pi. The customer Android Client connect To Java Server, together with post something together with then unopen socket. This illustration present how Android Client connect to Java Server (run on raspberry Pi) together with proceed connected, together with post to together with have from Server.


For the Server side, it’s Java Echo Server to post dorsum the received information to the sender.

Please notice:
Suppose the Disconnect push is used to disconnect from server in 1 trial clicked. In this example, it cannot actually. Because the code bufferedReader.readLine() in ClientThread.java volition block the program. The Disconnect asking volition await until anything received to provide from bufferedReader.readLine().

Create ClientThread.java to grip network related project inwards background thread.

package com.blogspot.android_er.androidclient;  import android.os.Message;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket;  world degree ClientThread extends Thread{      String dstAddress;     int dstPort;     somebody boolean running;     MainActivity.ClientHandler handler;      Socket socket;     PrintWriter printWriter;     BufferedReader bufferedReader;      world ClientThread(String addr, int port, MainActivity.ClientHandler handler) {         super();         dstAddress = addr;         dstPort = port;         this.handler = handler;     }      world void setRunning(boolean running){         this.running = running;     }      somebody void sendState(String state){         handler.sendMessage(                 Message.obtain(handler,                         MainActivity.ClientHandler.UPDATE_STATE, state));     }      world void txMsg(String msgToSend){         if(printWriter != null){             printWriter.println(msgToSend);         }     }      @Override     world void run() {         sendState("connecting...");          running = true;          endeavor {             socket = novel Socket(dstAddress, dstPort);             sendState("connected");              OutputStream outputStream = socket.getOutputStream();             printWriter = novel PrintWriter(outputStream, true);              InputStream inputStream = socket.getInputStream();             InputStreamReader inputStreamReader =                     novel InputStreamReader(inputStream);             bufferedReader = novel BufferedReader(inputStreamReader);              while(running){                  //bufferedReader block the code                 String business = bufferedReader.readLine();                 if(line != null){                     handler.sendMessage(                             Message.obtain(handler,                                     MainActivity.ClientHandler.UPDATE_MSG, line));                 }              }          } select have of (IOException e) {             e.printStackTrace();         } endure {             if(bufferedReader != null){                 endeavor {                     bufferedReader.close();                 } select have of (IOException e) {                     e.printStackTrace();                 }             }              if(printWriter != null){                 printWriter.close();             }              if(socket != null){                 endeavor {                     socket.close();                 } select have of (IOException e) {                     e.printStackTrace();                 }             }         }          handler.sendEmptyMessage(MainActivity.ClientHandler.UPDATE_END);     } }  

MainActivity.java

package com.blogspot.android_er.androidclient;  import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  world degree MainActivity extends AppCompatActivity {      EditText editTextAddress, editTextPort, editTextMsg;     Button buttonConnect, buttonDisconnect, buttonSend;     TextView textViewState, textViewRx;      ClientHandler clientHandler;     ClientThread clientThread;      @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);         buttonDisconnect = (Button) findViewById(R.id.disconnect);         buttonSend = (Button)findViewById(R.id.send);         textViewState = (TextView)findViewById(R.id.state);         textViewRx = (TextView)findViewById(R.id.received);          buttonDisconnect.setEnabled(false);         buttonSend.setEnabled(false);          buttonConnect.setOnClickListener(buttonConnectOnClickListener);         buttonDisconnect.setOnClickListener(buttonDisConnectOnClickListener);         buttonSend.setOnClickListener(buttonSendOnClickListener);          clientHandler = novel ClientHandler(this);     }      View.OnClickListener buttonConnectOnClickListener =             novel View.OnClickListener() {                  @Override                 world void onClick(View arg0) {                      clientThread = novel ClientThread(                             editTextAddress.getText().toString(),                             Integer.parseInt(editTextPort.getText().toString()),                             clientHandler);                     clientThread.start();                      buttonConnect.setEnabled(false);                     buttonDisconnect.setEnabled(true);                     buttonSend.setEnabled(true);                 }             };      View.OnClickListener buttonDisConnectOnClickListener = novel View.OnClickListener() {         @Override         world void onClick(View v) {             if(clientThread != null){                 clientThread.setRunning(false);             }          }     };      View.OnClickListener buttonSendOnClickListener = novel View.OnClickListener() {         @Override         world void onClick(View v) {             if(clientThread != null){                 String msgToSend = editTextMsg.getText().toString();                 clientThread.txMsg(msgToSend);             }         }     };      somebody void updateState(String state){         textViewState.setText(state);     }      somebody void updateRxMsg(String rxmsg){         textViewRx.append(rxmsg + "\n");     }      somebody void clientEnd(){         clientThread = null;         textViewState.setText("clientEnd()");         buttonConnect.setEnabled(true);         buttonDisconnect.setEnabled(false);         buttonSend.setEnabled(false);      }      world static degree ClientHandler extends Handler {         world static concluding int UPDATE_STATE = 0;         world static concluding int UPDATE_MSG = 1;         world static concluding int UPDATE_END = 2;         somebody MainActivity parent;          world ClientHandler(MainActivity parent) {             super();             this.parent = parent;         }          @Override         world void handleMessage(Message msg) {              switch (msg.what){                 instance UPDATE_STATE:                     parent.updateState((String)msg.obj);                     break;                 instance UPDATE_MSG:                     parent.updateRxMsg((String)msg.obj);                     break;                 instance UPDATE_END:                     parent.clientEnd();                     break;                 default:                     super.handleMessage(msg);             }          }      } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     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="https://rootandroid.org//"         android:textStyle="bold" />     <EditText         android:id="@+id/address"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="192.168."         android:hint="dstAddress" />     <EditText         android:id="@+id/port"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="8000"         android:hint="dstPort" />     <Button         android:id="@+id/connect"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Connect"/>     <Button         android:id="@+id/disconnect"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Disconnect"/>     <TextView         android:id="@+id/state"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="un-initiated"         android:textSize="20dp"/>     <EditText         android:id="@+id/msgtosend"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="msg to send..." />     <Button         android:id="@+id/send"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Send"/>     <TextView         android:id="@+id/received"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textSize="18dp"/> </LinearLayout>  

uses-permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml

 The customer Android Client connect To Java Server Android customer illustration 2, communicate amongst Java Server run on Raspberry PiDownload the files .