C# 尝试从扫描仪加载信息时Android应用程序崩溃

C# 尝试从扫描仪加载信息时Android应用程序崩溃,c#,android,C#,Android,我正在开发一个应用程序,需要扫描条形码,然后通过TCPip端口将其发送到C#服务器。单个应用程序可以工作,但当我尝试读取变量时,通常会显示并发送它(扫描完成后,变量会更新)。 获取此错误: 致命异常:主 进程:com.example.android.miniclient,PID:11351 java.lang.RuntimeException:传递结果失败 ResultInfo{who=null,request=0,result=-1,data=Intent{ act=com.google.zx

我正在开发一个应用程序,需要扫描条形码,然后通过TCPip端口将其发送到C#服务器。单个应用程序可以工作,但当我尝试读取变量时,通常会显示并发送它(扫描完成后,变量会更新)。 获取此错误:

致命异常:主 进程:com.example.android.miniclient,PID:11351 java.lang.RuntimeException:传递结果失败 ResultInfo{who=null,request=0,result=-1,data=Intent{ act=com.google.zxing.client.android.SCAN flg=0x80000(有额外功能)} 活动 {com.example.android.miniclient/com.example.android.miniclient.MainActivity}: android.os.NetworkOnMainThreadException

Android应用程序的代码如下: 要从reader应用程序读取的代码:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                //get the extras that are returned from the intent
                contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Toast toast = Toast.makeText(this, "Código Leido:" + contents + " Formato:" + format, Toast.LENGTH_LONG);
                //connectSocket("Hello");
                toast.show();
            }
        }
    }

TCPip通信代码:

 public  void connectSocket(String a){

        try {
            InetAddress serverAddr = InetAddress.getByName("192.168.1.223");
            Log.d("TCP", "C: Connecting...");
            Socket socket = new Socket(serverAddr, 11000);
            a="contents";
            String message = a;

            PrintWriter out = null;
            BufferedReader in = null;

            try {

用读取的代码更新显示的代码:

public void updateTextView (String finalText) {


        textView4.setText(finalText);

        return;
    }

我已经准备好尝试从其他公共空间更新TextView,也尝试更改更新变量的时间。但保持同样的问题。读卡器独立工作,服务器/用户独立工作

我需要扫描结果显示,并用于作为变量调用


感谢您的帮助

您无法在主(UI)线程上处理网络逻辑。正如你所看到的,你得到了这个

因此,无法在UI线程上运行此方法: 公共空连接套接字(字符串a){

请阅读Android文档中的更多信息:

检查:

  • 等等
还有很多不错的LIB,比如:


BR!

您无法在主(UI)线程上处理网络逻辑。正如您所看到的,您得到了这一点

因此,无法在UI线程上运行此方法: 公共空连接套接字(字符串a){

请阅读Android文档中的更多信息:

检查:

  • 等等
还有很多不错的LIB,比如:

比尔

try {
    InetAddress serverAddr = InetAddress.getByName("192.168.1.223");
    Log.d("TCP", "C: Connecting...");
    Socket socket = new Socket(serverAddr, 11000);
    a="contents";
    String message = a;

    PrintWriter out = null;
    BufferedReader in = null;

    try {