JAVA服务端android客户端如何通信
假如我java服务端是在虚拟机linux上开发,android客户端在windows上开发,那他们如何通信???,用json吧,我觉得json比较好看..就例如在andr...
假如我java服务端是在虚拟机linux上开发,android客户端在windows上开发,那他们如何通信???,用json吧,我觉得json比较好看..就例如在android客户端登陆,通过java服务端在数据库上查找,android注册用户通过java服务端上插入数据库。有这方面的例子给我参考吗?
展开
4个回答
展开全部
一、HTTP请求(APACHE的HttpClient实现)
服务器端,就是普通的servlet、Strutus2就可以
移动端
protected static String get(String url, List<NameValuePair> params) {
String resultMsg;
// 设置http请求配置
HttpParams parms = new BasicHttpParams();
parms.setParameter("charset", HTTP.UTF_8);
// 配置连接超时
HttpConnectionParams.setConnectionTimeout(parms, 10 * 1000);
// 设置请求超时
HttpConnectionParams.setSoTimeout(parms, 15 * 1000);
// 实例化HttpClient
HttpClient httpclient = new DefaultHttpClient(parms);
// 实例化HttpGet
HttpGet httpget = new HttpGet(url);
// 设置请求头
httpget.addHeader("Content-Type", "application/json");
httpget.addHeader("charset", HTTP.UTF_8);
try {
if (params.size() > 0)
url = url + "?" + URLEncodedUtils.format(params, HTTP.UTF_8);
HttpResponse resp = httpclient.execute(httpget);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "连接异常";
}
} catch (Exception e) {
resultMsg = "连接异常";
} finally {
// 关闭get
httpget.abort();
// 关闭连接 ,释放资源
httpclient.getConnectionManager().shutdown();
}
return resultMsg;
}
protected static String post(String uri, Object params) {
String resultMsg;
// 设置http请求配置
HttpParams hp = new BasicHttpParams();
hp.setParameter("charset", HTTP.UTF_8);
// 配置连接超时
HttpConnectionParams.setConnectionTimeout(hp, 10 * 1000);
HttpConnectionParams.setSoTimeout(hp, 15 * 1000);
// 实例化HttpClient
HttpClient httpclient = new DefaultHttpClient(hp);
// 实例化HttpPost请求
HttpPost httppost = new HttpPost(uri);
// 设置头信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("charset", HTTP.UTF_8);
try {
// 将参数进行json化
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(params);
Log.i(TAG, "URI=" + uri + ",BEAN=" + jsonStr);
// 定义消息实体
StringEntity se = new StringEntity(jsonStr, HTTP.UTF_8);
httppost.setEntity(se);
// 通信
HttpResponse resp = httpclient.execute(httppost);
int statusCode = resp.getStatusLine().getStatusCode();
Log.i(TAG, "StatusCode=" + statusCode);
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "连接异常";
}
} catch (Exception e) {
e.printStackTrace();
resultMsg = "连接异常";
} finally {
// 关闭get
httppost.abort();
// 关闭连接 ,释放资源
httpclient.getConnectionManager().shutdown();
}
Log.i(TAG, resultMsg);
return resultMsg;
}
二、SOCKET连接
服务器端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class service_java_test {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10000); //绑定的端口号
Socket socket = server.accept(); //连接不成功以至于下一行的"连接成功"
//在调试区显示不出来
System.out.println("连接成功");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = in.readLine();
System.out.println(msg);
out.println("Server received " + msg); //向接收方发送已接受到了的语句
out.flush();
if (msg.equals("bye")) { //若接收到"bye"则break
break;
}
}
socket.close();
}
}
安卓客户端:
package com.example.t4_android;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.textView1);
Thread t = new Thread(new Runnable(){
public void run(){
try {
Socket sk = new Socket("192.168.253.1", 10000);//绑定套接字,这一行一直执行不成功
//以至于下一行在安卓页面的TextView上
//不显示“已连接”的字样
//"192.168.253.1"是我利用DOS命令查找
//的本机IP
myTextView.setText("已连接");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
服务器端,就是普通的servlet、Strutus2就可以
移动端
protected static String get(String url, List<NameValuePair> params) {
String resultMsg;
// 设置http请求配置
HttpParams parms = new BasicHttpParams();
parms.setParameter("charset", HTTP.UTF_8);
// 配置连接超时
HttpConnectionParams.setConnectionTimeout(parms, 10 * 1000);
// 设置请求超时
HttpConnectionParams.setSoTimeout(parms, 15 * 1000);
// 实例化HttpClient
HttpClient httpclient = new DefaultHttpClient(parms);
// 实例化HttpGet
HttpGet httpget = new HttpGet(url);
// 设置请求头
httpget.addHeader("Content-Type", "application/json");
httpget.addHeader("charset", HTTP.UTF_8);
try {
if (params.size() > 0)
url = url + "?" + URLEncodedUtils.format(params, HTTP.UTF_8);
HttpResponse resp = httpclient.execute(httpget);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "连接异常";
}
} catch (Exception e) {
resultMsg = "连接异常";
} finally {
// 关闭get
httpget.abort();
// 关闭连接 ,释放资源
httpclient.getConnectionManager().shutdown();
}
return resultMsg;
}
protected static String post(String uri, Object params) {
String resultMsg;
// 设置http请求配置
HttpParams hp = new BasicHttpParams();
hp.setParameter("charset", HTTP.UTF_8);
// 配置连接超时
HttpConnectionParams.setConnectionTimeout(hp, 10 * 1000);
HttpConnectionParams.setSoTimeout(hp, 15 * 1000);
// 实例化HttpClient
HttpClient httpclient = new DefaultHttpClient(hp);
// 实例化HttpPost请求
HttpPost httppost = new HttpPost(uri);
// 设置头信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("charset", HTTP.UTF_8);
try {
// 将参数进行json化
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(params);
Log.i(TAG, "URI=" + uri + ",BEAN=" + jsonStr);
// 定义消息实体
StringEntity se = new StringEntity(jsonStr, HTTP.UTF_8);
httppost.setEntity(se);
// 通信
HttpResponse resp = httpclient.execute(httppost);
int statusCode = resp.getStatusLine().getStatusCode();
Log.i(TAG, "StatusCode=" + statusCode);
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "连接异常";
}
} catch (Exception e) {
e.printStackTrace();
resultMsg = "连接异常";
} finally {
// 关闭get
httppost.abort();
// 关闭连接 ,释放资源
httpclient.getConnectionManager().shutdown();
}
Log.i(TAG, resultMsg);
return resultMsg;
}
二、SOCKET连接
服务器端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class service_java_test {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10000); //绑定的端口号
Socket socket = server.accept(); //连接不成功以至于下一行的"连接成功"
//在调试区显示不出来
System.out.println("连接成功");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = in.readLine();
System.out.println(msg);
out.println("Server received " + msg); //向接收方发送已接受到了的语句
out.flush();
if (msg.equals("bye")) { //若接收到"bye"则break
break;
}
}
socket.close();
}
}
安卓客户端:
package com.example.t4_android;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.textView1);
Thread t = new Thread(new Runnable(){
public void run(){
try {
Socket sk = new Socket("192.168.253.1", 10000);//绑定套接字,这一行一直执行不成功
//以至于下一行在安卓页面的TextView上
//不显示“已连接”的字样
//"192.168.253.1"是我利用DOS命令查找
//的本机IP
myTextView.setText("已连接");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
展开全部
从服务器写一个字符串过去可以这样:String p = "-1,2,5";
发过去以后,将字符串分隔开,String[] ss = p.split(",");
数组里面的元素就是位置的三个坐标了。
但鉴于你可能发送多种类型的数据,你可以写一个协议,举例说就是,String p ="P,-1,2,5";
分隔出来以后,可以根据数组里面的第一个值判断是什么类型的数据,在执行相关的操作。
发过去以后,将字符串分隔开,String[] ss = p.split(",");
数组里面的元素就是位置的三个坐标了。
但鉴于你可能发送多种类型的数据,你可以写一个协议,举例说就是,String p ="P,-1,2,5";
分隔出来以后,可以根据数组里面的第一个值判断是什么类型的数据,在执行相关的操作。
追问
答非所问。。。
追答
好吧 我是看到没有人给你回答 就去网上给你找了一下 就找到这些了 就给你发上去了 你竟然 无语了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用webservice开放数据接口,格式可以是json也可以是xml,如果觉得webservice麻烦的话,直接用out.prilnt()将数据库里的数据打印到页面上来,打印格式一般用json比较好,android客户端只需用httpclient或者scoket连接网络访问打印数据页面即可把数据抓取呈现在手机客户端
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用socket通信。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询