Android 客户端与 Python 服务器无法建立连接

开发人员尝试通过套接字搭建一个简单的 Python 服务器与 Android 应用程序之间的连接。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();
}   }

Python 服务器代码:

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 unexpectedly. Please try again.”

  1. 解决方案

要解决此问题,开发人员需要确保在同一个线程中进行服务器调用和向用户界面返回结果。可以使用 Handler 或考虑使用 Android 中的 Asynctask。AsyncTAsk 允许在该方法被触发后向界面写数据。

以下是在 onPostExecute(Result) 方法中更新 UI 的代码示例:

@Override
protected void onPostExecute(Result result) {
    // Update the UI here
    TextView receivedMsg = (TextView) findViewById(R.id.textView2);
    receivedMsg.setText(result);
}

使用这种方法,开发者可以在 onPostExecute(Result) 方法中向界面写数据,而无需担心线程同步问题。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值