在将此标记为重复之前,请听我说。我已经从StackOverflow的成员那里得到了以前的建议,但我仍然无法使用它,但是我已经改进了我的代码并将其解决了可能存在的问题但是我不明白它是什么我做错了。仅供参考:这是我正在制作的客户端 - 服务器聊天程序的客户端。我已经制作了完全正常运行的服务器和客户端的PC版本。
我认为可能是问题的根源。如果你看一下ServerTask类(按下连接按钮时执行),它看起来好像没有启动套接字?有人可以请我帮忙吗?如果有人可以帮助这个绝望的初学者,那将是非常感激。
这可能没有用,但这里也是AndroidManifest.xml,因为我看到了我需要设置权限的地方。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.JurkoAndroidChat"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
package com.example.JurkoAndroidChat;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import java.net.*;
import java.io.*;
import java.util.*;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
// Right here, we connecting the components of the main.xml form to code
Button connectButton, disconnectButton, sendButton;
TextView chatArea, clientArea;
EditText messageField, usernameField, ipField;
//Extra variables and sockets
String username, serverIP;
int Port = 5000;
Socket sock;
PrintWriter out;
BufferedReader in;
ArrayList<String> userList;
Boolean isConnected = false;
ServerTask serverTask;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("Working?");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userList = new ArrayList();
connectButton = (Button)findViewById(R.id.button);
sendButton = (Button)findViewById(R.id.button1);
disconnectButton = (Button)findViewById(R.id.button2);
chatArea = (TextView)findViewById(R.id.textView2);
clientArea = (TextView)findViewById(R.id.textView3);
messageField = (EditText)findViewById(R.id.editText2);
usernameField = (EditText)findViewById(R.id.editText);
ipField = (EditText)findViewById(R.id.editText1);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
if (isConnected == false) {
username = usernameField.getText().toString();
usernameField.setFocusable(false);
usernameField.setClickable(false);
serverIP = ipField.getText().toString();
ipField.setFocusable(false);
ipField.setClickable(false);
serverTask = new ServerTask();
serverTask.execute();
} else if (isConnected == true) {
chatArea.append("You are already connected to the server.\n");
}
}
});
disconnectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
String bye = (username + ": :Disconnect");
try {
out.print(bye);
out.flush();
chatArea.append("Disconnected.\n");
sock.close();
} catch (Exception e) {e.printStackTrace();}
isConnected = false;
usernameField.setFocusable(true);
usernameField.setClickable(true);
ipField.setFocusable(true);
ipField.setClickable(true);
clientArea.setText("");
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//To change body of implemented methods use File | Settings | File Templates.
String nothing = "";
if ((messageField.getText().toString().equals(nothing))) {
messageField.setText("");
messageField.requestFocus();
} else {
try {
out.println(username + ":" + messageField.getText().toString() + ":" + "Chat");
out.flush();
serverTask.printToStream("hey");
} catch (Exception e) {
chatArea.append("Message was not sent.\n" + e);
}
messageField.setText("");
messageField.requestFocus();
}
}
});
}
public class ServerTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
Log.i("Asynctask", "doInBackground");
sock = new Socket(serverIP, Port);
out = new PrintWriter(sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.println(username + ":" + "has connected." + ":" + "Connect");
out.flush();
isConnected = true;
} catch (Exception ex) {
// chatArea.append("Unable to connect to " + serverIP + " at port " + Port + "." + ex);
// ex.printStackTrace();
// usernameField.setFocusable(true);
// usernameField.setClickable(true);
// ipField.setFocusable(true);
// ipField.setClickable(true);
}
new streamTask().execute();
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void printToStream(String message) {
try {
out.println(message);
out.flush();
} catch (Exception e) {chatArea.append(e + "\n"); }
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//To change body of overridden methods use File | Settings | File Templates.
}
}
public class streamTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String[] data;
String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
try {
while ((stream = in.readLine()) != null) {
data = stream.split(":");
if (data[2].equals(chat)) {
chatArea.append(data[0] + ": " + data[1] + '\n');
} else if (data[2].equals(connect)) {
chatArea.setText("");
userAdd(data[0]);
} else if (data[2].equals(disconnect)) {
userRemove(data[0]);
} else if (data[2].equals(done)) {
clientArea.setText("");
writeUsers();
userList.clear();
}
}
} catch (Exception e) {e.printStackTrace();}
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
public void executeTask() {
new ServerTask().execute();
}
public class IncomingReader implements Runnable {
@Override
public void run() {
//To change body of implemented methods use File | Settings | File Templates.
}
}
public void ListenThread() {
Thread IncomingReader = new Thread(new IncomingReader());
IncomingReader.start();
}
public void userAdd(String data) {
userList.add(data);
}
public void userRemove(String data) {
chatArea.append(data + " has disconnected from the server.\n");
for (String token:userList)
if (token.equals(data))
userList.remove(token);
}
public void writeUsers() {
String[] tempList = new String[(userList.size())];
userList.toArray(tempList);
for (String token:tempList) {
clientArea.append(token + '\n');
}
}
}
答案 0 :(得分:0)
由于没有日志(我建议您提供日志以便我们为您提供帮助),因此不确定您究竟是什么问题,但是当我查看您的代码时,我发现有一个大问题:
您尝试在后台线程中更新UI
请参阅以下代码行:
protected Void doInBackground(Void... voids) {
String[] data;
String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
try {
while ((stream = in.readLine()) != null) {
data = stream.split(":");
if (data[2].equals(chat)) {
chatArea.append(data[0] + ": " + data[1] + '\n');
} else if (data[2].equals(connect)) {
chatArea.setText("");
userAdd(data[0]);
} else if (data[2].equals(disconnect)) {
userRemove(data[0]);
} else if (data[2].equals(done)) {
clientArea.setText("");
writeUsers();
userList.clear();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null; // To change body of implemented methods use File |
// Settings | File Templates.
}
有些地方你在这个doInBackground中更新了UI,它在后台线程中执行,并会导致异常。
阅读这篇文章,这可能有助于https://developer.android.com/training/multiple-threads/communicate-ui.html
答案 1 :(得分:0)
对于每个遇到AsyncTask问题的人来说都是这样,因为它可能听起来令人困惑,它确实有效。我终于开始工作,我不知道为什么localhost没有工作,因为它是我正在测试的物理设备(三星Galaxy Tab 2 7.0)。我决定尝试我的实际外部IP,它终于奏效了。
构建您的应用程序,以便在doInBackground中完成物理网络内容(无GUI)仍然完成UI工作但我相信它可以通过runOnUIThread或在同一任务中完成但在onPostExecute中