gpt4 book ai didi

java - Android 客户端无法正确向服务器发送消息

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:23 24 4
gpt4 key购买 nike

我正在为 Android 设备上的客户端编写代码,以将消息发送到服务器,并且服务器应该回复它。该布局由编辑文本字段、按钮和 TextView 组成。当按下按钮时,应从编辑文本字段中取出消息并将其发送到服务器,当服务器收到消息时,应回复一条消息,表明已收到该消息,然后该回复消息将由服务器接收客户端并写在 TextView 上。问题是,当我按两次按钮时,消息会发送到服务器,然后当我第三次按时,服务器崩溃。任何有关问题所在的帮助将不胜感激。提前致谢。

这是我收到的 logcat 错误

02-04 04:18:38.065: I/Error51(32228): android.os.NetworkOnMainThreadException

MainActivity.java

package com.example.testclientandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

static Socket socket;
static final int SERVERPORT = 50000;
static final String SERVER_IP = "192.168.0.105";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Thread(new ClientThread()).start();

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
EditText editText = (EditText) findViewById(R.id.editText1);
TextView textView = (TextView) findViewById(R.id.textView2);
try {
String str = editText.getText().toString();

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(str);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
textView.setText(in.readLine());

} catch (UnknownHostException e) {
Log.i("Error47", e.toString());
} catch (IOException e) {
Log.i("Error49", e.toString());
} catch (Exception e) {
Log.i("Error51", e.toString());
}
}
});
}

private static class ClientThread implements Runnable {

public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e) {
Log.i("Error64", e.toString());
} catch (IOException e) {
Log.i("Error65", e.toString());
}
}

}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Send" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="53dp"
android:text="Response:"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" />

</RelativeLayout>

Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testclientandroid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testclientandroid.MainActivity"
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>

</manifest>

服务器类.java

package mainPackage;
//Server
import java.net.*;
import java.io.*;

public class ServerClass
{

static final int PORTNUMBER = 50000;

public static void main(String[] args) throws IOException
{
new Thread(new ServerThread()).start();
}

public static class ServerThread implements Runnable {

ServerSocket serverSocket;
Socket clientSocket;
public void run() {
try {
serverSocket = new ServerSocket(PORTNUMBER);
clientSocket = serverSocket.accept();
new Thread(new CommunicationThread(clientSocket)).start();

} catch (IOException e1) {
e1.printStackTrace();
}
}
}

public static class CommunicationThread implements Runnable {

Socket socket;
BufferedReader in;
PrintWriter out;

public CommunicationThread(Socket clientSocket) {

socket = clientSocket;

try {

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
System.out.println(e.toString());
}
}

public void run() {
try {

while(in.readLine() != null)
{
System.out.println(in.readLine());
out.println(in.readLine() + " Received");
}

} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}

更新我尝试使用 asyncTask,我不再收到 Logcat 错误,但现在服务器根本没有收到消息,

修改后的代码如下:

package com.example.testclientandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

static Socket socket;
static final int SERVERPORT = 50000;
static final String SERVER_IP = "192.168.0.105";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Thread(new ClientThread()).start();

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
new CommunicationTask().execute();
}
});
}

private static class ClientThread implements Runnable {

public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e) {
Log.i("Error64", e.toString());
} catch (IOException e) {
Log.i("Error65", e.toString());
}
}
}

private class CommunicationTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
EditText editText = (EditText) findViewById(R.id.editText1);
String result;
try {
String str = editText.getText().toString();

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(str);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
result = in.readLine();
return result;
} catch (UnknownHostException e) {
Log.i("Error47", e.toString());
} catch (IOException e) {
Log.i("Error49", e.toString());
} catch (Exception e) {
Log.i("Error51", e.toString());
}
return "Error";
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText(result);
}
}
}

最佳答案

任何需要互联网的操作都应该在后台完成。尝试阅读Asynctask消除此错误。

关于java - Android 客户端无法正确向服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com