因为最近实验室项目要求实现在局域网下将android app数据发送到winsock中进行保存,所以对此进行了简单学习。pc端因为是另一个同学做的,所以不做说明。
在android端,首先添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
之后对app界面进行简单设计——main.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定义一个文本框,它用于接收用户的输入 -->
<EditText
android:id="@+id/input"
android:layout_width="280dp"
android:layout_height="wrap_content" android:inputType=""/>
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:text="@string/send"/>
</LinearLayout>
<!-- 定义一个文本框,它用于显示来自服务器的信息 -->
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:background="#ffff"
android:textSize="14sp"
android:textColor="#f000"/>
界面如下:

创建ClientThread.java用于实现局域网下数据的传输:
public class ClientThread implements Runnable
{
private Socket s;
// 定义向UI线程发送消息的Handler对象
private Handler handler;
// 定义接收UI线程的消息的Handler对象
public Handler revHandler;
// 该线程所处理的Socket所对应的输入流
BufferedReader br = null;
OutputStream os = null;
public ClientThread(Handler handler)
{
this.handler = handler;
}
public void run()
{
try
{
s = new Socket("192.168.1.133", 8040);
br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
os = s.getOutputStream();
// 启动一条子线程来读取服务器响应的数据
new Thread()
{
@Override
public void run()
{
String content = null;
// 不断读取Socket输入流中的内容
try
{
while ((content = br.readLine()) != null)
{
// 每当读到来自服务器的数据之后,发送消息通知程序
// 界面显示该数据
Message msg = new Message();
msg.what = 0x123;
msg.obj = content.toString();
handler.sendMessage(msg);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
// 为当前线程初始化Looper
Looper.prepare();
// 创建revHandler对象
revHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
// 接收到UI线程中用户输入的数据
if (msg.what == 0x345)
{
// 将用户在文本框内输入的内容写入网络
try
{
os.write((msg.obj.toString() + "\r\n")
.getBytes("utf-8"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
};
// 启动Looper
Looper.loop();
}
catch (SocketTimeoutException e1)
{
System.out.println("网络连接超时!!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
最后创建MainActivity.java实现app中发送内容的获取等功能:
public class MainActivity extends Activity
{
// 定义界面上的两个文本框
EditText input;
TextView show;
// 定义界面上的一个按钮
Button send;
Handler handler;
// 定义与服务器通信的子线程
ClientThread clientThread;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input = (EditText) findViewById(R.id.input);
send = (Button) findViewById(R.id.send);
show = (TextView) findViewById(R.id.show);
handler = new Handler() // ②
{
@Override
public void handleMessage(Message msg)
{
// 如果消息来自于子线程
if (msg.what == 0x123)
{
// 将读取的内容追加显示在文本框中
show.append("\n" + msg.obj.toString());
}
}
};
clientThread = new ClientThread(handler);
// 客户端启动ClientThread线程创建网络连接、读取来自服务器的数据
new Thread(clientThread).start(); // ①
send.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
// 当用户按下发送按钮后,将用户输入的数据封装成Message
// 然后发送给子线程的Handler
Message msg = new Message();
msg.what = 0x345;
msg.obj = input.getText().toString();
clientThread.revHandler.sendMessage(msg);
// 清空input文本框
input.setText("");
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
这样,就实现了我们想要的传输数据到pc中的功能了。
原文:https://www.cnblogs.com/jlutiger/p/8974711.html