安卓发送数据到服务端

安卓端发送数据到服务端。

      安卓端使用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>










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值