我在学习安卓,用Socket实现数据传输。
我看的Android智能穿戴设备开发从入门到精通。服务器端
是java工程,代码如下:
public class AndroidServer implements Runnable {
public void run(){
try{
ServerSocket serverSocket=new ServerSocket(54321);
while(true)
{
System.out.println("等待接收用户连接:");
Socket client=serverSocket.accept();
try
{
BufferedReader in=new BufferedReader(new
InputStreamReader(client.getInputStream()));
String str=in.readLine();
System.out.println("read: "+str);
PrintWriter out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(client.getOutputStream())),true);
out.println("return "+str);
in.close();
out.close();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
client.close();
System.out.println("close");
}
}
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread desktopServerThread=new Thread(new AndroidServer());
desktopServerThread.start();
}
}
客户端是安卓工程,布局文件如下:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="14dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginRight="14dp"
android:layout_marginTop="67dp"
android:text="发送" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="39dp"
android:text="1" />
一个编辑框、一个按钮、一个文本框。
源代码如下:
public class MainActivity extends ActionBarActivity {
private TextView text1;
private Button but1;
private EditText edit1;
private final String DEBUG_TAG="mySocketAct";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.textView2);
but1=(Button)findViewById(R.id.button1);
edit1=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Socket socket=null;
String mesg=edit1.getText().toString()+"
";
edit1.setText("");
Log.e("dddd", "sent id");
try{
socket =new Socket("192.168.1.101",54321);
PrintWriter out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
BufferedReader br=new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String mstr=br.readLine();
if(mstr!=null)
{
text1.setText(mstr);
}else
{
text1.setText("数据错误");
}
out.close();
br.close();
socket.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e)
{
Log.e(DEBUG_TAG, e.toString());
}
}
});
}
AndroidManifest.xml添加了权限:
<uses-permission android:name="android.permission.INTERNET"/>
此帖出自
Linux与安卓论坛
一周热门 更多>