private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new ConnectTask().execute(ip, String.valueOf(port)); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } private class ConnectTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { String ip = params[0]; int port = Integer.parseInt(params[1]); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 获取SeekBar对象 SeekBar progressBar = findViewById(R.id.progressBar); // 添加OnSeekBarChangeListener监听器 progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; int tnum = 0; tnum = number; out.println(tnum); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } private void showSendMessageDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_send_message, null); final EditText messageEditText = view.findViewById(R.id.messageEditText); builder.setView(view) .setTitle("发送消息") .setPositiveButton("发送", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = messageEditText.getText().toString(); if (out != null) { new Thread(new Runnable() { @Override public void run() { out.println(message); } }).start(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); }代码有问题
时间: 2024-02-26 18:53:21 浏览: 123
代码中存在一些问题:
1. 在 ConnectTask 类的 doInBackground 方法中,如果出现异常,不会抛出异常,也不会给用户任何反馈,需要添加一些异常处理代码。
2. 在 onDestroy 方法中,关闭 in、out 和 socket 时,应该先判断它们是否为 null,避免空指针异常。
3. 在 showSendMessageDialog 方法中,如果 out 为 null,应该给用户提示信息,告诉用户连接未建立。
4. 在 sendMessageButton 的点击事件中,不应该在子线程中调用 out 的 println 方法,因为 out 是在 doInBackground 方法中创建的,不在主线程中,需要使用 Handler 将消息发送到主线程再执行。
5. SeekBar 的 progress 值只能是 0 到 10,但是在 onProgressChanged 方法中,有计算 progress * 10,这是不正确的。
6. SeekBar.OnSeekBarChangeListener 中的方法 onStartTrackingTouch 和 onStopTrackingTouch 可以不用实现。
最后,建议将 Handler 定义在 Activity 中,避免内存泄漏。
相关问题
public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); messageTextView = findViewById(R.id.messageTextView); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 在主页面中的窗口中实时显示消息 new Thread(new Runnable() { @Override public void run() { try { while (true) { String message = in.readLine(); if (message == null) { break; } handler.post(new Runnable() { @Override public void run() { messageTextView.setText(message+"\n"); } }); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } });能帮我把代码中的在主页面实时显示消息的功能换成滑动一个半圆进度条,按进度条百分比发送数字的功能吗
当您滑动半圆形进度条时,您需要将进度值转换为数字,并将其发送到服务器上。为此,您需要修改 "HalfCircleProgressBar" 类的 "set_value" 方法,以便在进度值改变时发送数字。
以下是修改后的代码:
```
import tkinter as tk
import math
import socket
import threading
class HalfCircleProgressBar(tk.Canvas):
def __init__(self, master, radius, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.radius = radius
self.arc_start = 90
self.arc_extent = 0
self.create_arc(self.coords(), start=self.arc_start, extent=self.arc_extent, style=tk.ARC, width=10)
self.bind('<B1-Motion>', self.set_value)
self.socket = None
self.out = None
def coords(self):
x0 = y0 = self.radius
x1 = y1 = self.radius * 3
return x0, y0, x1, y1
def set_socket(self, host, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
self.out = self.socket.makefile(mode='w')
def set_value(self, event):
x, y = event.x, event.y
cx, cy = self.coords()[0], self.coords()[1]
if x <= cx:
self.arc_start = 180
else:
self.arc_start = 0
self.arc_extent = int(math.atan((cy - y) / (x - cx)) * 180 / math.pi)
if self.arc_start == 180:
self.arc_extent += 180
self.delete("all")
self.create_arc(self.coords(), start=self.arc_start, extent=self.arc_extent, style=tk.ARC, width=10)
percentage = int(self.arc_extent / 180 * 100)
if self.out is not None: # Send value to server
self.out.write(str(percentage) + '\n')
self.out.flush()
root = tk.Tk()
progress_bar = HalfCircleProgressBar(root, radius=50, width=100, height=100)
progress_bar.pack()
def receive_message():
while True:
try:
message = progress_bar.socket.recv(1024).decode('utf-8')
progress_bar.update_message(message)
except ConnectionAbortedError:
break
def connect_to_server():
host = 'localhost' # Replace with your server host
port = 8000 # Replace with your server port
progress_bar.set_socket(host, port)
threading.Thread(target=receive_message).start()
tk.Button(root, text="Connect", command=connect_to_server).pack()
root.mainloop()
```
在这个例子中,我们将 "HalfCircleProgressBar" 类稍作修改,使其能够与服务器进行通信。在 "set_socket" 方法中,我们创建了一个新的套接字并连接到服务器上。在 "set_value" 方法中,我们将进度值转换为数字,并将其写入套接字中发送到服务器上。在 "receive_message" 函数中,我们从服务器接收消息,并将其传递给 "HalfCircleProgressBar" 类的 "update_message" 方法来更新消息。
在主函数中,我们创建了一个 "HalfCircleProgressBar" 对象并将其添加到主窗口中。我们还创建了两个函数: "connect_to_server" 和 "receive_message"。当用户单击 "Connect" 按钮时,我们调用 "set_socket" 方法来连接到服务器上,并启动一个新的线程来接收从服务器发回的消息。
请注意,这个例子中的服务器连接代码是一个简单的示例,您需要根据自己的需求来修改。
package com.example.ksswj; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); messageTextView = findViewById(R.id.messageTextView); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 在主页面中的窗口中实时显示消息 while (true) { String message = in.readLine(); if (message == null) { break; } handler.post(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }我还需要在上面代码中加入一个对话框,能够向端口发送消息,能帮我加上代码吗
当然可以,你需要在 layout 文件夹下创建一个新的 xml 文件作为对话框的布局,比如命名为 dialog_send_message.xml。然后在 MainActivity 中添加一个方法,用来弹出对话框并向端口发送消息。代码如下:
```java
package com.example.ksswj;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private EditText ipEditText;
private EditText portEditText;
private TextView messageTextView;
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private Handler handler = new Handler();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipEditText = findViewById(R.id.ipEditText);
portEditText = findViewById(R.id.portEditText);
messageTextView = findViewById(R.id.messageTextView);
Button connectButton = findViewById(R.id.connectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ip = ipEditText.getText().toString();
int port = Integer.parseInt(portEditText.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 在主页面中的窗口中实时显示消息
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
handler.post(new Runnable() {
@Override
public void run() {
messageTextView.append(message + "\n");
}
});
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
Button sendMessageButton = findViewById(R.id.sendMessageButton);
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSendMessageDialog();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void showSendMessageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_send_message, null);
final EditText messageEditText = view.findViewById(R.id.messageEditText);
builder.setView(view)
.setTitle("发送消息")
.setPositiveButton("发送", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String message = messageEditText.getText().toString();
if (out != null) {
out.println(message);
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create()
.show();
}
}
```
其中,`showSendMessageDialog()` 方法用来弹出对话框,`dialog_send_message.xml` 文件作为对话框的布局文件。在对话框中,用户可以输入要发送的消息,然后点击“发送”按钮将消息发送到端口上。
阅读全文