數據發送回與UI我有這樣的AsyncTask,我用它來在互聯網上發送聊天信息。問題是,當我執行任務時,什麼事都沒有發生 - 至少不是在UI上。我懷疑onProgressUpdate()
根本不執行。這個想法是,當任務開始時,一條消息將通過互聯網發送,用戶界面上的EditText將用新文本更新。這是全班同學:無法從的AsyncTask
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import android.os.AsyncTask;
import android.widget.EditText;
public class Messager extends AsyncTask<SocketAndEditText, Void, Void> {
private MulticastSocket socket;
private EditText host;
private EditText port;
private EditText sendMessage;
private EditText messageBoard;
private InetAddress serverAddress;
private int pt;
private String newConverstion;
private String message;
@Override
protected Void doInBackground(SocketAndEditText... soEd) {
// get the text that they contain and add the new messages to the old ones
//host = soEd[0].getHost();
//port = soEd[0].getPort();
messageBoard = soEd[0].getMessageBoard();
sendMessage = soEd[0].getSendMessage();
message = sendMessage.getText().toString();
String conversation = messageBoard.getText().toString();
newConverstion = conversation.concat("\n[You] ").concat(message);
return null;
}
protected void onProgressUpdate(Integer... progress) {
// make the messages text view editable
messageBoard.setFocusable(true);
messageBoard.setText(newConverstion); // add the new message to the text view
messageBoard.setFocusable(false); // make the messages text view not editable
// erase the text on the second text view that has just been sent
sendMessage.setText("");
sendMessage(message);
}
public void sendMessage(String message) {
// convert the host name to InetAddress
try {
serverAddress = InetAddress.getByName("localhost");
} catch (Exception e) {}
pt = 4456;
// create socket and start communicating
try {
socket = new MulticastSocket(pt);
socket.joinGroup(serverAddress);
} catch (IOException e) {}
// Send message to server
// convert message to bytes array
byte[] data = (message).getBytes();
// create and send a datagram
DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, pt);
try {
socket.send(packet);
} catch (IOException e) {}
}
}
出了什麼問題?
Здрасти,имашлипредставакоеточновкоиметодидасложа? ТезиAsyncTasksнапълномеобъркахасвсичкитеимметодиипараметри。 МеждудруготоможешлидапрепоръчашнякаквочетивозамрежовопрограмираненаАндроид,чеазненамирампочтинищосериозно。 Мерсиипоздрави。 – RegedUser00x 2012-01-29 12:56:21
@ RegedUser00x:我被抓到了,我真的是保加利亞人。我也很喜歡我的語言,並且更喜歡說它。但是,這是社區網站,您應該同意我們的語言,雖然特殊,但不適用於SO的官方語言。翻譯你的文章: '嗨,你知道在哪種方法中放置什麼? AsyncTask類將我的所有方法都弄糊塗了。順便說一下,你可以指點我一些書來了解Android中的網絡編程,因爲我幾乎找不到任何東西。 Thanks.' – 2012-01-29 13:34:35