我是通过XCOM v2.0 发送信息,移动端接收信息后也可以发信息过去,界面如下(比较简单自己写就行)
与硬件通信肯定免不了解析Json,使用Gson解析简单方便,在解析过程中遇到了一个坑爹的问题,解析出错了(;′⌒`),我在电脑输入下面字符串
{\"name\":\"John\", \"age\":20,\"grade\":{\"course\":\"English\",\"score\":100,\"level\":\"A\"}}
\"是代表 "这个符号,所以我就从XCOM v2.0发送上面字符串过来,解析失败。。。后来debug发现发送过来的信息是直接当字符串处理,不会再转义,也就\"是代表\",
所以只需发送下面格式信息即可解析
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 mEditText = (EditText) findViewById(R.id.edittext); 8 connectButton = (Button) findViewById(R.id.connectbutton); 9 sendButton = (Button) findViewById(R.id.sendbutton); 10 mTextView = (TextView) findViewById(R.id.retextview); 11 12 13 //连接按钮 14 connectButton.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View view) { 17 new Thread() { 18 @Override 19 public void run() { 20 super.run(); 21 try { 22 clientSocket = new Socket("192.168.23.6", 3000); 23 mReceiveThread = new ReceiveThread(clientSocket); 24 mReceiveThread.start(); 25 }catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 }.start(); 30 displayToast("连接成功"); 31 stop = false; 32 } 33 }); 34 //发送数据 35 sendButton.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View view) { 38 byte[] msgBuffer = null; 39 40 String text = mEditText.getText().toString(); 41 42 try { 43 msgBuffer = text.getBytes("GB2312"); 44 outStrean = clientSocket.getOutputStream(); 45 outStrean.write(msgBuffer); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 mTextView.setText(text); 50 displayToast("发送成功"); 51 } 52 }); 53 54 } 55 56 private void displayToast(String s) { 57 Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 58 } 59 60 private class ReceiveThread extends Thread { 61 private InputStream inStrean = null; 62 private byte[] buf11 = new byte[1024]; 63 private String str11 = null; 64 65 public ReceiveThread(Socket s) { 66 try { 67 this.inStrean = s.getInputStream(); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 @Override 74 public void run() { 75 super.run(); 76 while (!stop) { 77 try { 78 this.inStrean.read(buf11); 79 str11 = new String(buf11,"GB2312"); 80 Gson gson = new Gson(); 81 JsonReader reader = new JsonReader(new StringReader(str11)); 82 reader.setLenient(true); 83 Student student = gson.fromJson(reader,Student.class); 84 85 String name = student.getName(); 86 // System.out.println(student.toString()); 87 Message msg = new Message(); 88 msg.obj = name; 89 mHandle.sendMessage(msg); 90 } catch (IOException e) { 91 e.printStackTrace(); 92 } 93 } 94 } 95 } 96 97 98 @Override 99 public void onDestroy() { 100 super.onDestroy(); 101 if (mReceiveThread != null) { 102 try { 103 clientSocket.close(); 104 stop = true; 105 mReceiveThread.interrupt(); 106 } catch (IOException e) { 107 e.printStackTrace(); 108 } 109 } 110 } 111 }
下载说明
☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,
☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!
☉唯一站长QQ:1004003180 [人格担保-本站注重诚信!]
☉购买建议E-mail:1004003180@qq.com 源码收购 E-mail:1004003180@qq.com
☉本站文件解压密码 【文章内都自带解压密码,每个密码不同!】