首页/ 行业新闻/ 技术分享/ PDA利用http连接erp系统
PDA可以通过安装APP实现与ERP系统的对接,通常情况下PDA使用wifi连接网络后就能获得网络连接的能力,借助http就可以把数据传输给ERP接口并接收返回数据。本文介绍PDA应用程序开发过程中使用post的方式发送条码给ERP系统接口服务器,并获得返回数据。
大家都知道,现在主流的PDA都采用安卓系统,所以PDA更像是一个拥有独立扫描头的安卓手机。既然运行在安卓系统之下,那我们就可以使用安卓开发来完成PDA和ERP系统对接的专用APP。今天我们重点分享的是如何利用http协议来完成PDA和ERP系统的对接,但是由于篇幅有限,只对http协议中使用到的post操作进行分析。本次分享的内容是建立在模拟环境下完成,使用Java搭建一个模拟ERP系统接口服务器,并且模拟PDA在局域网内发送条码信息给接口服务器。
整个模拟过程非常简单,只为分析数据如何从PDA发送给接口服务器,并且PDA接收接口服务器返回的信息。正常情况下PDA接收到接口服务器返回信息后,应该对返回信息做对应的处理,比如盘点单处理成功后PDA应该给客户一个操作成功的提示。
启动接口服务器后,我们就可以开始模拟PDA向ERP系统接口服务器发送条码数据。
这个时候接口服务器会接收到条码数据,并且在模拟环境中返回了一条已收到条码的信息给PDA。
以上就是本次模拟PDA发送条码数据给接口服务器后,接口服务器向PDA返回处理信息的全部流程,下面我们把关键代码贴出来。代码仅适应于模拟环境,如果需要在生产环境使用需要进行调整。希望可以帮到大家。
post部分:
public class PostUtils {
public static String doPost(String httpUrl, String param){
JSONObject responseJson = new JSONObject();
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开连接
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("Connection","Close");
// 设置连接请求方式
connection.setRequestMethod("POST");
// 设置连接主机服务器超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取主机服务器返回数据超时时间:60000毫秒
connection.setReadTimeout(60000);
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
connection.setDoOutput(true);
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
connection.setDoInput(true);
// 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
connection.setRequestProperty("Content-type", "text/json");
// 通过连接对象获取一个输出流
os = connection.getOutputStream();
// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
os.write(param.getBytes());
// 通过连接对象获取一个输入流,向远程读取
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
// 循环遍历一行一行读取数据
while ((temp = br.readLine()) != null) {
sbf.append(temp);
}
responseJson = new JSONObject(sbf.toString());
}else{
responseJson.put("status","error");
responseJson.put("message","返回数据出错。");
responseJson.put("msgCode",connection.getResponseCode()+"");
}
} catch (MalformedURLException e) {
responseJson.put("status","error");
responseJson.put("message","返回数据出错。\t"+e.getMessage());
responseJson.put("msgCode","506");
} catch (IOException e) {
responseJson.put("status","error");
responseJson.put("message","返回数据出错。\t"+e.getMessage());
responseJson.put("msgCode","507");
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
responseJson.put("status","error");
responseJson.put("message","关闭连接出错。\t"+e.getMessage());
responseJson.put("msgCode","500");
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
responseJson.put("status","error");
responseJson.put("message","关闭连接出错。\t"+e.getMessage());
responseJson.put("msgCode","500");
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
responseJson.put("status","error");
responseJson.put("message","关闭连接出错。\t"+e.getMessage());
responseJson.put("msgCode","500");
}
}
// 断开与远程地址url的连接
if (connection != null) {
connection.disconnect();
}
}
return responseJson.toString();
}
}
如何使用post:
JSONObject requestJson = new JSONObject();
requestJson.put("barcode","6600202311010110");
updateTextArea(getDateTimeString()+"PDA向接口服务器发送条码数据。");
updateTextArea(getDateTimeString()+PostUtils.doPost("http://192.168.123.123:6688/chajianyun/pdaTest",requestJson.toString()));