如何让我的服务器处理http POST请求?

时间:2016-09-22 21:14:38

标签: java http post request

我的服务器:

public class Servertest implements HttpHandler {


protected static String server_IP;

public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {


    int server_Port = 1337;


    try {

        InetAddress iAddress = InetAddress.getLocalHost();
        server_IP = String.valueOf(iAddress.getHostAddress());
        System.out.println("Server IP address : " + server_IP);
    } catch (UnknownHostException e) {
    }

    ServerSocket serverSocket = new ServerSocket(server_Port);


    while (true) {
        Socket socket = serverSocket.accept();
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os, true);
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        pw.println("Connection confirmed ");
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();

        pw.println("your ip address is " + str);

        pw.close();
    }
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
    String response = "it works!";
    httpExchange.sendResponseHeaders(200, response.length());
    System.out.println(response);
    OutputStream os = httpExchange.getResponseBody();
    os.write(response.getBytes());
    os.close();
}}

我的POST请求:

 @Override
public void onLocationChanged(Location location) {
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    mMap.animateCamera(cameraUpdate);
    RequestQueue queue = Volley.newRequestQueue(this);
    final String lat = String.valueOf(location.getLatitude());
    final String longi = String.valueOf(location.getLongitude());
    StringRequest sr = new StringRequest(Request.Method.POST, "http://192.168.87.101:1337", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            System.out.println(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("error");
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("lat", lat);
            params.put("longi", longi);

            return params;
        }

    };

所以我的问题是如何在服务器上处理我的帖子请求? 我可以打开服务器并打印pw.println(&#34; Connection确认&#34;);和pw.println(&#34;你的IP地址是&#34; + str);但我不能打印我的回复。

0 个答案:

没有答案