Python服务器与Android应用程序的连接

2024-06-09 04:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一个问题。我一直在寻找类似问题的解决方案,但在每一个案例中,都有一些不同于我的案例。 我试图在Python服务器和使用sockets的Android应用程序之间建立一个简单的连接。Android应用程序启动与服务器的对话:它向服务器发送消息,服务器接收并显示消息,然后服务器向应用程序发送回复。应用程序在屏幕上以文本视图显示回复。 这是我在客户端的代码:

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();
}   }

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

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()

当我尝试从应用程序向服务器发送消息时,它工作得很好。但是,一旦服务器收到并显示消息,应用程序立即停止,并显示错误消息:has stopped unexpectly。请再试一次。 附加信息:我使用adt bundle进行Android开发,并使用IDLE运行服务器代码。都在8号窗口。


Tags: the代码服务器应用程序消息newinputpublic
2条回答

据我所知,您使用一个线程来调用服务器,但在同一个线程中,您试图将结果回传给UI。

final TextView receivedMsg=(TextView)查找视图yid(R.id.textView2); receivedMsg.setText(br.toString())

如果使用自己的Java线程,则必须在自己的代码中处理以下需求: 如果将结果回传给用户界面,则与主线程同步。 我看不出你在这么做。你要么必须使用一个处理程序,要么也许你应该考虑使用android的异步任务。使用AsyncTAsk,可以在触发此方法后在UI中编写。 onPostExecute(结果) 后台计算完成后在UI线程上调用。

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

您正在非GUI线程中写入GUI对象。您需要使用处理程序才能将消息传递回GUI线程。

这里有一个相当简单的例子:Update UI through Handler

相关问题 更多 >