Android Customer To Post Message To Coffee Server On Raspberry Pi

Posted on

It’s a Java customer in addition to server practice inward my roughly other blogspot HelloraspberryPi. The customer connect to server in addition to post something, the server uncomplicated impress the received message in addition to close.

It’s Android version of the client, to connect to server in addition to post something.

package com.blogspot.android_er.androidclient;  import android.os.AsyncTask; import android.os.Bundle; import; import android.view.View; import android.widget.Button; import android.widget.EditText;  import; import; import; import; import;  populace degree 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(;         editTextPort = (EditText) findViewById(;         editTextMsg = (EditText) findViewById(;         buttonConnect = (Button) findViewById(;          buttonConnect.setOnClickListener(buttonConnectOnClickListener);     }      View.OnClickListener buttonConnectOnClickListener =             novel View.OnClickListener() {                  @Override                 populace void onClick(View arg0) {                     MyClientTask myClientTask = novel MyClientTask(                             editTextAddress.getText().toString(),                             Integer.parseInt(editTextPort.getText().toString()));                     myClientTask.execute(editTextMsg.getText().toString());                 }             };      populace degree 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) {             endeavor {                 Socket socket = novel Socket(dstAddress, dstPort);                  OutputStream outputStream = socket.getOutputStream();                 PrintStream printStream = novel PrintStream(outputStream);                 printStream.print(params[0]);                  socket.close();              } grab (UnknownHostException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }             provide null;         }      } }  


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""     xmlns: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=""         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 in addition to send..."/>  </LinearLayout>  

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

The Java code of the server side, hollo to “Java Network exercise: customer in addition to server – customer post something to server“.

The side past times side event “Android customer event 2, communicate amongst Java Server run on Raspberry Pi” present how Android Client connect to Java Server (run on raspberry Pi) in addition to proceed connected, in addition to post to in addition to have from Server.