Connection between Python Server and Android Application

This is my first question. I was looking for solutions to such problems, but in each case there were some differences compared to my case. I am trying to establish a simple connection between a Python server and an Android application using sockets. The Android application starts a conversation with the server: it sends a message to the server, the server receives and displays it, and then the server sends a response to the application. The application displays the response on the screen in TextView. This is my client side code:

public class MyClient extends Activity implements OnClickListener{ EditText enterMessage; Button sendbutton; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.myclient); enterMessage = (EditText)findViewById(R.id.enterMessage); sendbutton = (Button)findViewById(R.id.sendbutton); sendbutton.setOnClickListener(this); } @Override public void onClick(View arg0) { Thread t = new Thread(){ @Override public void run() { try { Socket s = new Socket("192.168.183.1", 7000); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(enterMessage.getText().toString()); //read input stream DataInputStream dis2 = new DataInputStream(s.getInputStream()); InputStreamReader disR2 = new InputStreamReader(dis2); BufferedReader br = new BufferedReader(disR2);//create a BufferReader object for input //print the input to the application screen final TextView receivedMsg = (TextView) findViewById(R.id.textView2); receivedMsg.setText(br.toString()); dis2.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } }; t.start(); Toast.makeText(this, "The message has been sent", Toast.LENGTH_SHORT).show(); } } 

And on the server side, this is my code:

 from socket import * HOST = "192.168.183.1" #local host PORT = 7000 #open port 7000 for connection s = socket(AF_INET, SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) #how many connections can it receive at one time conn, addr = s.accept() #accept the connection print "Connected by: " , addr #print the address of the person connected while True: data = conn.recv(1024) #how many bytes of data will the server receive print "Received: ", repr(data) reply = raw_input("Reply: ") #server reply to the client conn.sendall(reply) conn.close() 

When I try to send a message from the application to the server, it works fine. However, as soon as the server receives the message and displays it, the application immediately stops with the error message: it unexpectedly stopped. Please try again. Additional info: I use adt-bundle for Android development and IDLE for running server code. Both on Windows8.

+6
source share
2 answers

From what I understand, you are using a thread to call the server, but in the same thread you are trying to send the results to the user interface.

final TextView receivedMsg = (TextView) findViewById (R.id.textView2); receivedMsg.setText (br.toString ());

If you use your own Java stream, you need to fulfill the following requirements in your own code: Synchronization with the main stream if you send the results to the user interface. I do not see that you are doing this. You need to either use a handler, or perhaps you should use Asynctask for android. With AsyncTAsk, you can write in the user interface after running this method. onPostExecute (Result) Called in the user interface thread after background calculations have completed.

So, inside this method you can write in the user interface. take a look at these links http://learningdot.diandian.com/post/2014-01-02/40060635109 Asynctask vs Thread in android

+2
source

You are writing a GUI object in a non gui stream. You need to use a handler to pass the message back to the GUI thread.

Take a look at a fairly simple example here: Update user interface through a handler

0
source

All Articles