我已经实现了在我的PC上运行的Java TCP服务器,该服务器将接受从也已实现的运行客户端的Android设备发送的传入消息。服务器只需要从Android设备接收此消息并将其显示在其窗口中即可。我能够成功连接(稍后我解释为什么会这样),但是我无法发送消息。这是我遇到错误的代码部分:
while (fgets (string, STRSIZE, stdin))
我已经使用Log.e进行了测试,但不满足条件
out!= null
如果我删除'!= null'条件,我得到的错误如下:
for (;;)
在主要活动中,我们有:
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
在TCPClient类中,我们有:
07-07 23:20:00.581 4368-4368/delfino.tchau_papeletas D/AndroidRuntime: Shutting down VM
07-07 23:20:00.585 4368-4368/delfino.tchau_papeletas E/AndroidRuntime: FATAL EXCEPTION: main
Process: delfino.tchau_papeletas, PID: 4368
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6259)
at android.view.View$PerformClick.run(View.java:24732)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6259)
at android.view.View$PerformClick.run(View.java:24732)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.PrintWriter.checkError()' on a null object reference
at delfino.tchau_papeletas.TCPClient.sendMessage(TCPClient.java:84)
at delfino.tchau_papeletas.MainActivity.Fazer_Upload(MainActivity.java:187)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6259)
at android.view.View$PerformClick.run(View.java:24732)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
在服务器中,我们有:
public TCPClient mTcpClient;
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
public void messageReceived(String message) {
}
});
mTcpClient.run();
return null;
}
}
//called when Upload ImageButton is clicked
public void Upload(View view){
String = Name.getText().toString();
new connectTask().execute("");
if (mTcpClient != null) {
mTcpClient.sendMessage(String);
ImageButton Upload = (ImageButton)findViewById(R.id.Upload);
Upload.setEnabled(false);
}
}
此“完成!”正确显示在窗口中(在服务器中):
public class TCPServer extends Thread {
public PrintWriter out;
public void run() {
mRun = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (mRun) {
}
Log.e("RESPONSE FROM SERVER", "S: Mensagem recebida: '" + serverMessage + "'");
} catch (Exception e) {
} finally {
socket.close();
}
} catch (Exception e) {
}
}
public void sendMessage(String message){
if (out!= null && !out.checkError()) {
out.println(message);
out.flush();
}
}
所以我非常有信心连接已经稳定。
我不知道为什么它表现为空,因为我将其作为全局变量并将其实例化:
@Override
public void run() {
super.run();
running = true;
try {
messageListener.messageReceived("Server: Waiting...\n");
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
Socket client = serverSocket.accept();
messageListener.messageReceived("Server: Done!\n");
try {
mOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while (running) {
}
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
} finally {
client.close();
System.out.println("S: End!");
}
} catch (Exception e) {
System.out.println("S: Erro");
e.printStackTrace();
}
}
关于如何使此“输出”正常工作的任何想法?预先感谢!
答案 0 :(得分:0)
我不知道出了什么问题,所以这不是一个完整的答案,但是我设法通过其他方式做到这一点,因此这将来可能会对某些人有所帮助。
我不再在android中使用第二个Java TCP类,而是在MainActivity中像这样做所有事情:
public class connectTask extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
socket = new Socket(ip,port);
printwriter = new PrintWriter(socket.getOutputStream());
printwriter.write(mensagem);
printwriter.flush();
printwriter.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
被这样称呼的:
connectTask myTask = new connectTask();
myTask .execute();
无需花费很多精力,因此非常简单。通过在EditText中获取文本来发出发送的消息,如下所示:
mensagem = Codigo.getText().toString();
我浪费了很多时间尝试调试代码,但是有时值得重新开始。我遵循了此视频教程here,我显然推荐它,因为它具有我需要的一切。
再次感谢您的所有帮助。