Android客户端与服务器的数据交互
这是今年7月份时在一家公司实训时自己做的一个小项目,仅仅是一个简单的模拟Android客户端与服务器的数据交互,中间会有串口通信,以及利用JDBC连接Mysql数据库的相关代码。其中网络通信采用的是HTTP协议,串口通信使用的是开源RXTX包实现。
Android客户端:
- 登录界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" style="@style/AppTheme" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/t1" android:gravity="center_vertical" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="@string/UserName" android:textColor="#00ffff" android:textColorHighlight="#00ffff" style="@style/AppTheme" android:textSize="20sp" /> <EditText android:id="@+id/Username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:textColor="#ffffff" android:textColorHighlight="#00ffff" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="@string/Password" android:textColor="#00ffff" android:textColorHighlight="#00ffff" android:textSize="20sp" /> <EditText android:id="@+id/Password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:inputType="textPassword" android:textColor="#ffffff" android:textColorHighlight="#00ffff" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/btn_Login" style="@style/AppTheme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Login" android:textColor="#d9d919" android:textSize="20sp" /> <Button android:id="@+id/btn_Reset" style="@style/AppTheme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Reset" android:textColor="#d9d919" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
- 在开始业务逻辑之前先加上访问网络的授权:
<uses-permission android:name="android.permission.INTERNET" />
- 登录业务代码:
package com.zhou.android; import java.lang.ref.WeakReference; import com.zh