繁体   English   中英

在Android设备上打开套接字

[英]Socket Opening on Android Device

我知道这似乎是一个重复的问题,但实际上并非如此。 我想通过套接字与一些传感器通信。 该连接在Emulator上工作正常,但是当我在Android设备上将其上传时,它将无法正常工作。 调试后,我将应用程序缩小为使用以下代码简单地打开套接字。

try
{
 Socket socket = new Socket(InetAddress.getByName(ipAddress).getHostAddress(), 10001);
}
catch (Exception e)
{
 Toast.makeText(context, "Error: "+e.getMessage(), duration).show();
}

我只是打开一个套接字。这在仿真器上可以正常工作,但是当我在设备上运行该应用程序时,它挂了一段时间,甚至没有显示异常消息,却什么也没告诉我...任何想法可能是问题所在。 (最初是StrictMode问题,但我进行了梳理)。 LogCat上没有错误。 我已经安装了android终端模拟器并尝试ping操作,回复很好。 提前致谢


这是代码

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);
          }
    }
}

这在模拟器上可以正常工作,但在设备上不能正常工作。

也许是因为您尝试在主UI线程中建立连接。 看起来您将上述代码段放在了Activity类中。

您应该创建一个单独的线程来与网络进行通信。

使用简单Thread或Android出色的AsyncTask

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM