安卓跟java通讯_安卓App和java通信实例

服务器:放在电脑上运行的java文件

48304ba5e6f9fe08f3fa1abda7d326ab.png

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class MyServer implements Runnable{

//服务器连接

public static ServerSocket serverSocket;

//连接

public static Socket socket;

//端口

public static final int PORT = 8888;

public void run() {

DataInputStream dis = null;

DataOutputStream dos = null;

try {

serverSocket = new ServerSocket(PORT);

System.out.println("正在等待客户端连接...");

//这里处于等待状态,如果没有客户端连接,程序不会向下执行

while(true){

socket = serverSocket.accept();

dis = new DataInputStream(socket.getInputStream());

dos = new DataOutputStream(socket.getOutputStream());

//读取数据

String clientStr = dis.readUTF();

//写出数据

dos.writeUTF(clientStr);

System.out.println("----客户端已成功连接!----");

//得到客户端的IP

System.out.println("客户端的IP =" + socket.getInetAddress());

//得到客户端的端口号

System.out.println("客户端的端口号 =" + socket.getPort());

//得到本地端口号

System.out.println("本地服务器端口号=" + socket.getLocalPort());

System.out.println("-----------------------");

System.out.println("客户端:" + clientStr);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {//我们把流的关闭写在finally里,即使读写出现问题,我们也能正常的关闭流!

try {

if (dis != null)

dis.close();

if (dos != null)

dos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void main(String[] args){

Thread desktopServerThread = new Thread(new MyServer());

desktopServerThread.start();

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

App工程文件:

1、AndroidManifest.xml(主xml文件)

48304ba5e6f9fe08f3fa1abda7d326ab.png

package="com.himi" android:versionCode="1" android:versionName="1.0">

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、res/values/strings.xml(资源文件)

48304ba5e6f9fe08f3fa1abda7d326ab.png

这里输入文字发给服务器

SocketConnect

发送

这里显示服务器发来的信息!

48304ba5e6f9fe08f3fa1abda7d326ab.png

3、res/layout/main.xml(资源文件)

48304ba5e6f9fe08f3fa1abda7d326ab.png

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_height="wrap_content" android:text="@string/hello" />

android:layout_height="wrap_content" />

android:layout_height="wrap_content" android:text="@string/send" />

android:layout_height="wrap_content" android:text="@string/get" />

48304ba5e6f9fe08f3fa1abda7d326ab.png

4、src/com.himi/MainActivity.java(Activity文件,客户端端主程序)

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.himi;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.Socket;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

/**

* @author Himi

*/

public class MainActivity extends Activity implements OnClickListener {

private Button btn_ok;

private EditText edit;

private TextView tv;

//Socket用于连接服务器获取输入输出流

private Socket socket;

//服务器server/IP地址

private final String ADDRESS = "10.203.8.167";

//服务器端口

private final int PORT = 8888;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main);

btn_ok = (Button) findViewById(R.id.Btn_commit);

tv = (TextView) findViewById(R.id.tv);

edit = (EditText) findViewById(R.id.edit);

btn_ok.setOnClickListener(this);

}

public void onClick(View v) {

if (v == btn_ok) {

DataInputStream dis = null;

DataOutputStream dos = null;

try {

//阻塞函数,正常连接后才会向下继续执行

socket = new Socket(ADDRESS, PORT);

//socket = new Socket("localhost", PORT);

dis = new DataInputStream(socket.getInputStream());

dos = new DataOutputStream(socket.getOutputStream());

//向服务器写数据

dos.writeUTF(edit.getText().toString());

String temp = "I say:";

temp += edit.getText().toString();

temp += "\n";

temp += "Server say:";

//读取服务器发来的数据

temp += dis.readUTF();

tv.setText(temp);

} catch (IOException e) {

Log.e("Himi", "Stream error!");

e.printStackTrace();

} finally {

try {

if (dis != null)

dis.close();

if (dos != null)

dos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值