Android 客户端将位置信息发送给服务端

一、题目

Android 客户端将位置信息发送给服务端

二、环境

Android Studio Eclipse

三、代码实现

客户端:

 HttpPost httpRequest = new HttpPost("http://192.168.159.1:8080/MyAndroidServer/ChildrenToServerServlet");
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
            Date date = new Date(System.currentTimeMillis());
            String str=simpleDateFormat.format(date);
            System.out.println(str);
            params.add(new BasicNameValuePair("Time", str));
            params.add(new BasicNameValuePair("Latitude",latitude));
            params.add(new BasicNameValuePair("Longitude", longitude));
            try {
                httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置请求参数项
                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse httpResponse = httpClient.execute(httpRequest);//执行请求返回响应
                if(httpResponse.getStatusLine().getStatusCode() == 200){//判断是否请求成功
//                    Toast.makeText(ChildrenToServerActivity.this, EntityUtils.toString(httpResponse.getEntity()), Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setAction("cn.abel.action.broadcast");
                    intent.putExtra("Response", EntityUtils.toString(httpResponse.getEntity()));
                    context.sendBroadcast(intent);
                }else{
//                    Toast.makeText(MainActivity.this, "没有获取到Android服务器端的响应!", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setAction("cn.abel.action.broadcast");
                    intent.putExtra("Response", "没有获取到Android服务器端的响应!");
                    context.sendBroadcast(intent);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

params.add(new BasicNameValuePair(“Time”, str));
Time是str的变量名,用于服务端接收数据用的。
这是用来添加要传递给服务端的数据,为String字符串形式。


服务端:

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setContentType("text/plain; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String time = request.getParameter("Time");
		String latitude = request.getParameter("Latitude");
		String longitude = request.getParameter("Longitude");
		ChildrenToAddressDao addressDao = new ChildrenToAddressDao();
		addressDao.insert(latitude, longitude, time);
		
		System.err.println(request.getParameter("Time"));
		System.err.println(request.getParameter("Latitude"));
		System.err.println(request.getParameter("Longitude"));
		PrintWriter printWriter = response.getWriter();
		printWriter.print("客户端你好,数据连接成功!");
		printWriter.flush();
		printWriter.close();
	}

request.getParameter(“变量名”)是用来接收客户端对应变量名的数据。
addressDao.insert()是我自己定义的方法,将接收到的数据存入MySQL中。

Android服务端将位置信息发送给客户端
可以看这篇博客。

有问题,欢迎留言交流!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值