Python服务器和Android应用程序之间的连接

sha*_*ati 7 python sockets eclipse android

这是我的第一个问题.我已经找到了类似问题的解决方案,但在每种情况下,与我的情况相比都存在一些差异.我试图使用套接字在Python服务器和Android应用程序之间建立一个简单的连接.Android应用程序启动与服务器的对话:它向服务器发送消息,服务器接收并显示它,然后服务器向应用程序发送回复.该应用程序在TextView中显示屏幕上的回复.这是我在客户端的代码:

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();
}   }
Run Code Online (Sandbox Code Playgroud)

在服务器端,这是我的代码:

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's reply to the client
    conn.sendall(reply)
conn.close()
Run Code Online (Sandbox Code Playgroud)

当我尝试从应用程序向服务器发送消息时,它可以正常工作.但是,只要服务器收到消息并显示它,应用程序就会立即停止并显示错误消息:意外停止.请再试一次.附加信息:我使用adt-bundle进行Android开发,使用IDLE运行服务器代码.两者都在Windows8上.

Per*_*sas 3

据我了解,您使用一个线程来调用服务器,但在同一个线程中您尝试将结果回发到 UI。

最终 TextView receiveMsg = (TextView) findViewById(R.id.textView2); receiveMsg.setText(br.toString());

如果您使用自己的 Java 线程,则必须在自己的代码中处理以下要求: 如果将结果回发到用户界面,则与主线程同步。我没有看到你正在这样做。你要么必须使用处理程序,要么你应该考虑使用android的Asynctask。使用 AsyncTAsk,您可以在触发此方法后在 UI 中写入。onPostExecute(Result) 后台计算完成后在 UI 线程上调用。

所以在这个方法中你可以在 UI 中编写。看看这些链接 http://learningdot.diandian.com/post/2014-01-02/40060635109 Asynctask vs Thread in android