Android client to send message to Java server on Raspberry Pi

Android client to send message to Java server on Raspberry Pi - Hello Friend GENERAL INFO FOR ANDROID DEVELOPMENT, In the article you read this time with the title Android client to send message to Java server on Raspberry Pi, We have prepared this article for you to read and retrieve information there in. Hopefully the contents of postings Artikel Android code sample: Networking, Artikel Android Network Programming, Artikel Network Programming, We write this you can understand. Alright, good read.

The Title : Android client to send message to Java server on Raspberry Pi
link : Android client to send message to Java server on Raspberry Pi

Read also


Android client to send message to Java server on Raspberry Pi


It's 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.

It's 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 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="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.



Thus Article Android client to send message to Java server on Raspberry Pi

The End Article Android client to send message to Java server on Raspberry Pi This time, hopefully can give benefits to all of you. well, see you in posting other articles.

You are now reading the article Android client to send message to Java server on Raspberry Pi with the link address http://xorislogo.blogspot.com/2016/05/android-client-to-send-message-to-java.html

0 Response to "Android client to send message to Java server on Raspberry Pi"

Post a Comment