安卓端发送数据到服务端。
安卓端使用json格式发送数据到服务端;服务端使用ssh框架。
首先安卓端发送数据的代码:
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Message msg = Message.obtain();
try {
HashMap map = new HashMap();
map.put("event", "123");
JSONObject req=new JSONObject();
/*for(Object okey:map.keySet()){
String key=okey.toString();
req.put(key, map.get(key));
}*/
req.put("startData", map);
//服务器URL <a target=_blank href="http://localhost:8080/updateInfo.html">http://localhost:8080/updateInfo.html</a>
URL url = null;
//url = new URL("http://192.168.1.104:8088/TimeManageServer/showEvent?phoneJsonStr=" + data);
url = new URL("http://192.168.1.104:8088/TimeManageServer/showEvent?startData=你好");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setRequestProperty("ser-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
JSONObject ClientKey = new JSONObject();
//ClientKey.put("username", "张三");
ClientKey.put("event", data);
// 接着我们使用JsonObject封装{"Person":{"username":"zhangsan","age":"12"}}
JSONObject Authorization = new JSONObject();
Authorization.put("startData", ClientKey);
//dos.writeBytes(URLEncoder.encode(req.toString(), "UTF-8"));
//dos.flush();
//dos.close();
//String content = String.valueOf(req);
String content = String.valueOf(ClientKey);
System.out.println("req:" + Authorization.toString());
os.write(content.getBytes());
os.flush();
os.close();
int code;
code = conn.getResponseCode();
String mes = conn.getResponseMessage();
responseCode = code;
System.out.println("code" + code);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb=new StringBuffer();
String line;
while((line=br.readLine())!=null)
sb.append(line);
br.close();
conn.disconnect();
JSONObject jsj = new JSONObject(sb.toString());
System.out.println("Str:" + jsj.toString());
if(code == 200){
msg.what = SAVE_SUCCESS;
}else{
msg.what = SAVE_FAIL;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//因为修改UI只能在主线程中可以进行,所以用handler进行修改UI.
handler.sendMessage(msg);
}
}
}.start();
附加handler代码(主要是为了显示一个对话框):
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what){
case SAVE_SUCCESS:
showresDialog(200);
break;
case SAVE_FAIL:
showresDialog(0);
break;
}
}
};
在ssh服务端接收数据:
对于接收的json数据向网上一样直接定义一个属性,然后有get、set方法,但没有好使不知道为什么,在这里直接用的HttpServletRequest request = ServletActionContext.getRequest();来获取安卓发送的json串。
HttpServletRequest request = ServletActionContext.getRequest();
StringBuffer sevents = new StringBuffer();
BufferedReader reader = request.getReader();
String input = null;
while((input = reader.readLine()) != null) {
sevents.append(input);
}
Struts配置文件:
<package name="myPkg0" extends="json-default" >
<action name="*Event" class="eventAction" method="{1}" >
<result name="success" type="json">
<param name="root">responseJson</param>
</result>
</action>
</package>