【问题标题】:Connection between Python Server and Android ApplicationPython 服务器和 Android 应用程序之间的连接
【发布时间】:2014-04-07 10:54:31
【问题描述】:

这是我的第一个问题。我一直在寻找类似问题的解决方案,但在每种情况下,与我的情况相比都有一些差异。 我正在尝试使用套接字在 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();
}   }

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

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

当我尝试从应用程序向服务器发送消息时,它运行良好。但是,一旦服务器收到消息并显示它,应用程序立即停止并显示错误消息:已意外停止。请再试一次。 附加信息:我使用 adt-bundle 进行 Android 开发和 IDLE 运行服务器代码。两者都在 Windows8 上。

【问题讨论】:

    标签: android python eclipse sockets


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 2022-09-30
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多