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";

出力:

  • ID | [123]

  • 何か他のもの | [1234]

  • 何か_else_else | [12345]


ソース | 参照: https://stackoverflow.com/a/5902142/1276374

于 2013-07-02T11:06:55.040 に答える