The Android client sends location information to the server

1. Title

The Android client sends location information to the server

2. Environment

Android Studio Eclipse

Three, code implementation

Client:

 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 is the variable name of str, used by the server to receive data.
This is used to add data to be passed to the server, in the form of String.


Server:

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("variable name") is used to receive data corresponding to the variable name of the client.
addressDao.insert() is a method defined by myself to store the received data in MySQL.

The Android server sends location information to the client.
You can read this blog.

If you have any questions, please leave a message to exchange!

Guess you like

Origin blog.csdn.net/weixin_43752257/article/details/113028470