0

我在我的设备中实现了一个带有服务器套接字的线程,它显示来自(例如)浏览器的传入连接。我已经正确找到IP了客户端的地址,但我无法找到 url 参数。你能给我帮助吗?请考虑这个字符串,例如:192.168.1.110:80/?id=123或者192.168.1.110/page?id=123 这是我的班级。

public class ServerThread implements Runnable {
    public void run(){
        try{
            if ( SERVERIP != null){
                serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable(){
                        @Override
                        public void run(){
                            serverStatus.setText("Connected");  
                        }
                    });
                    InetAddress ip_client = client.getInetAddress();
                    Log.i("Log", "ip client "+ip_client);
                    //Here i have to find the url params
                }
            }
        } catch (Exception e){
            serverStatus.setText("Error");
        }
    }
}

谢谢

4

1 回答 1

0
public class ServerThread implements Runnable {
    public void run(){
        try{
            if ( SERVERIP != null){
                serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable(){
                        @Override
                        public void run(){
                            serverStatus.setText("Connected");  
                        }
                    });
                    InetAddress ip_client = client.getInetAddress();
                    Log.i("Log", "ip client "+ip_client);
                    //Here i have to find the url params
                    //Find some way to convert your ip_client into string with all parameters together.. or do not use getInetAddress, but rather something easier.
                    LinkedHashMap<String, List<String>> parameters = getQueryParams("192.168.1.110:80/?id=123");  //use String address you get from client object
                }
            }
        } catch (Exception e){
            serverStatus.setText("Error");
        }
    }

    public static LinkedHashMap<String, List<String>> getQueryParams(String url) {
        //You can change to Map or HashMap if order of parameters does not matter for you
        try {
            LinkedHashMap<String, List<String>> params = new LinkedHashMap<String, List<String>>();
            String[] urlParts = url.split("\\?");
            if (urlParts.length > 1) {
                String query = urlParts[1];
                for (String param : query.split("&")) {
                    String[] pair = param.split("=");
                    String key = URLDecoder.decode(pair[0], "UTF-8");
                    String value = "";
                    if (pair.length > 1) {
                        value = URLDecoder.decode(pair[1], "UTF-8");
                    }

                    List<String> values = params.get(key);
                    if (values == null) {
                        values = new ArrayList<String>();
                        params.put(key, values);
                    }
                    values.add(value);
                }
            }

            return params;
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError(ex);
        }
    }
}

例子:

String query = "192.168.1.110:80/?id=123&something_else=1234&something_else_else=12345";

输出:

  • 编号 | [123]

  • 别的东西| [1234]

  • something_else_else | [12345]


来源 | 参考:https ://stackoverflow.com/a/5902142/1276374

于 2013-07-02T11:06:55.040 回答