Java Android调用TCP客户端方法
我通过在UI中按下按钮a来调用服务。作为回报,服务调用类中的TCP/IP客户端方法。服务启动时没有任何问题,但只要调用TCP/IP客户端方法,它就会使UI活动崩溃。我甚至尝试在没有服务的情况下直接调用此方法,但它仍然崩溃。此类未在清单中注册为活动,但我已在清单中声明了以下内容:Java Android调用TCP客户端方法,java,android,tcpclient,Java,Android,Tcpclient,我通过在UI中按下按钮a来调用服务。作为回报,服务调用类中的TCP/IP客户端方法。服务启动时没有任何问题,但只要调用TCP/IP客户端方法,它就会使UI活动崩溃。我甚至尝试在没有服务的情况下直接调用此方法,但它仍然崩溃。此类未在清单中注册为活动,但我已在清单中声明了以下内容: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="andro
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
服务
public class Tx extends Service {
Atx atx = new Atx();
@Override public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " TX Service Started", Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
String msg = extras.getString("EXTRA_M");
final String svrip = extras.getString("EXTRA_IP");
try {
atx.transmitAck(msg,svrip);
} catch (IOException e) {
e.printStackTrace();
}
stopSelf();
return START_NOT_STICKY;
}
public Tx() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
包含TCP/IP客户端方法的类
public class Atx {
public void transmitAck (String msg, String ipadress ) throws IOException {
int no_con_timeout =10000;
int port = 25901;
try {
InetAddress IPAddress =InetAddress.getByName(ipadress);
byte[] byteBuffer =msg.getBytes();
Socket client = new Socket();
client.connect(new InetSocketAddress(IPAddress, port), no_con_timeout);
client.setSoTimeout(500);
client.setTcpNoDelay(true);
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
out.write(byteBuffer); // Send the encoded string to the server
// Receive the same string back from the server
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd; // Bytes received in last read
while (totalBytesRcvd < byteBuffer.length) {
if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd,byteBuffer.length - totalBytesRcvd)) == -1)
throw new SocketException("Connection closed prematurely");
totalBytesRcvd +=bytesRcvd;}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
公共类Atx{
public void transmitAck(字符串msg、字符串ipAddress)引发IOException{
int no_con_timeout=10000;
int端口=25901;
试一试{
InetAddress IPAddress=InetAddress.getByName(IPAddress);
byte[]byteBuffer=msg.getBytes();
套接字客户端=新套接字();
连接(新的InetSocketAddress(IP地址,端口),无超时);
客户机设置时间(500);
client.setTcpNoDelay(true);
OutputStream out=client.getOutputStream();
InputStream in=client.getInputStream();
out.write(byteBuffer);//将编码的字符串发送到服务器
//从服务器接收相同的字符串
int totalBytesRcvd=0;//到目前为止接收到的总字节数
int bytesRcvd;//上次读取时接收的字节数
while(总字节数小于字节缓冲长度){
如果((bytesRcvd=in.read(byteBuffer,totalBytesRcvd,byteBuffer.length-totalBytesRcvd))=-1)
抛出新的SocketException(“连接过早关闭”);
totalBytesRcvd+=bytesRcvd;}
}捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
请让我知道我做错了什么?非常感谢您的帮助我通过在单独的线程上执行网络操作解决了这个问题 使用AsyncTask和doInBackground 有用的链接
您可以发布日志,显示您遇到的错误。我正在设备上测试,而不是在emmulator上测试
public class Atx {
public void transmitAck (String msg, String ipadress ) throws IOException {
int no_con_timeout =10000;
int port = 25901;
try {
InetAddress IPAddress =InetAddress.getByName(ipadress);
byte[] byteBuffer =msg.getBytes();
Socket client = new Socket();
client.connect(new InetSocketAddress(IPAddress, port), no_con_timeout);
client.setSoTimeout(500);
client.setTcpNoDelay(true);
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
out.write(byteBuffer); // Send the encoded string to the server
// Receive the same string back from the server
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd; // Bytes received in last read
while (totalBytesRcvd < byteBuffer.length) {
if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd,byteBuffer.length - totalBytesRcvd)) == -1)
throw new SocketException("Connection closed prematurely");
totalBytesRcvd +=bytesRcvd;}
}catch(IOException e)
{
e.printStackTrace();
}
}
}