[英]Socket Opening on Android Device
I know this seems a repeated question but it is not actually. 我知道这似乎是一个重复的问题,但实际上并非如此。 I want to communicate with some sensors over sockets. 我想通过套接字与一些传感器通信。 The connection work fine on Emulator but when I upload it on Android Device it wont work. 该连接在Emulator上工作正常,但是当我在Android设备上将其上传时,它将无法正常工作。 After Debugging I narrowed my app down to simple opening a socket using the following code. 调试后,我将应用程序缩小为使用以下代码简单地打开套接字。
try
{
Socket socket = new Socket(InetAddress.getByName(ipAddress).getHostAddress(), 10001);
}
catch (Exception e)
{
Toast.makeText(context, "Error: "+e.getMessage(), duration).show();
}
I am simply opening a socket.This works fine on the emulator but when I run this app on the device it hangs for some time and shows me nothing not even the exception message... Any Idea what could be the problem. 我只是打开一个套接字。这在仿真器上可以正常工作,但是当我在设备上运行该应用程序时,它挂了一段时间,甚至没有显示异常消息,却什么也没告诉我...任何想法可能是问题所在。 (Initially it was StrictMode problem but I sorted out). (最初是StrictMode问题,但我进行了梳理)。 No error on the LogCat. LogCat上没有错误。 I have installed android terminal emulator and tried ping, reply is fine. 我已经安装了android终端模拟器并尝试ping操作,回复很好。 Thanks in advance 提前致谢
This is the code 这是代码
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
Button btn;
private CharSequence text = "";
private Socket s;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
public void onClick(View view){
//detect the view that was "clicked"
switch(view.getId())
{
case R.id.button1:
new LongOperation().execute("");
break;
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
s = new Socket(InetAddress.getByName("o2215.airq.org").getHostAddress(), 10001);
text = "Connection Made";
} catch (Exception e) {
text = "Error : ";
for(int i=0;i<e.getStackTrace().length;i++)
{
text = text +"\n"+ e.getStackTrace()[i];
}
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText(text);
}
}
}
This works fine on the emulator, but not on the device. 这在模拟器上可以正常工作,但在设备上不能正常工作。
Perhaps, this is because you try to make a connection in the main UI thread. 也许是因为您尝试在主UI线程中建立连接。 Looks like you place the above code snippet inside your Activity
class. 看起来您将上述代码段放在了Activity
类中。
You should create a separate thread to make communications with network. 您应该创建一个单独的线程来与网络进行通信。
Use either simple Thread
or Android's wonderful AsyncTask
使用简单Thread
或Android出色的AsyncTask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.