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服务端将位置信息发送给客户端
可以看这篇博客。

有问题,欢迎留言交流!

猜你喜欢

转载自blog.csdn.net/weixin_43752257/article/details/113028470