Java 使用TCP+;AsyncTask保持连接打开并侦听从服务器发送的数据
大家好!这是我的问题。。。 1) 连接到TCP服务器*检查 2) 发送初始数据包并从服务器接收(VB.NET)*检查 现在我的问题 我正试图保持我的连接,并继续侦听传入的数据。我试着用计时器,但没有成功。任何帮助都将不胜感激Java 使用TCP+;AsyncTask保持连接打开并侦听从服务器发送的数据,java,android,android-intent,android-asynctask,inputstreamreader,Java,Android,Android Intent,Android Asynctask,Inputstreamreader,大家好!这是我的问题。。。 1) 连接到TCP服务器*检查 2) 发送初始数据包并从服务器接收(VB.NET)*检查 现在我的问题 我正试图保持我的连接,并继续侦听传入的数据。我试着用计时器,但没有成功。任何帮助都将不胜感激 package com.WheresmySon; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import jav
package com.WheresmySon;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class TcpClient extends Activity {
private static BufferedReader in;
private static BufferedWriter out;
private static Socket s;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new TcpClientTask().execute();
}
class TcpClientTask extends AsyncTask<Void, Void, Void> {
private static final int TCP_SERVER_PORT = 1234;
private boolean error = false;
Boolean SocketStarted = false;
protected Void doInBackground(Void... arg0) {
try {
s = new Socket("10.0.2.2", TCP_SERVER_PORT);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "VER:Android,LAT:28.111921,LNG:-81.950433,ID:1038263,SND:0,VDO:0,TXT:Testing";
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
//accept server response
String inMsg = in.readLine();
Log.i("TcpClient", "received: " + inMsg);
//close connection
} catch (UnknownHostException e) {
error = true;
e.printStackTrace();
} catch (IOException e) {
error = true;
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
if(error) {
// Something bad happened
}
else {
// Success
}
}
}
//replace runTcpClient() at onCreate with this method if you want to run tcp client as a service
private void runTcpClientAsService() {
Intent lIntent = new Intent(this.getApplicationContext(), TcpClientService.class);
this.startService(lIntent);
}
}
AsyncTask
不是任何网络设备的最佳场所,特别是对于像Sockets
这样的非静态连接。最好的方法是使用服务
。您可以尝试搜索任何关于如何在Android中使用服务处理套接字连接的代码示例。此代码永远不会正常工作。AsyncTask
不是任何网络设备的最佳位置,尤其是对于非静态连接,如套接字
。最好的方法是使用服务
。您可以尝试搜索任何关于如何在Android中使用服务处理套接字连接的代码示例。此代码永远不会正常工作。Hi Evos。如果您查看代码的底部,有一个runtcpclientservice(),我应该使用它吗?如果这是一个示例项目,并且它已经正确地实现了TcpClientService
,那么最好使用它而不是AsyncTask
.Evo。这是TcpClientServer(),假设我使用它,我将如何“监听”传入的数据并将此连接作为服务打开?@Ruben这是一个非常大的问题,它包含了许多关于编程和使用Android框架的信息。首先,检查有关服务()的文档,如果您不精通Android,您可以尝试以一种简单的方式使用它,如int this tutorial()Hi Evos。如果您查看代码的底部,有一个runtcpclientservice(),我应该使用它吗?如果这是一个示例项目,并且它已经正确地实现了TcpClientService
,那么最好使用它而不是AsyncTask
.Evo。这是TcpClientServer(),假设我使用它,我将如何“监听”传入的数据并将此连接作为服务打开?@Ruben这是一个非常大的问题,它包含了许多关于编程和使用Android框架的信息。首先,检查有关服务()的文档,如果您不精通Android,可以尝试以类似本教程()的简单方式使用它
package com.WheresmySon;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TcpClientService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
runTcpClient();
this.stopSelf();
}
private static final int TCP_SERVER_PORT = 1234;
private void runTcpClient() {
try {
Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
//accept server response
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}