2013-04-02 128 views
0

有沒有辦法將字符串參數傳遞給參數InetSocketAddress將字符串傳遞給InetSocketAddress

這是生成代理正確的方法:

SocketAddress addr = new InetSocketAddress("127.0.0.1", 9050); 
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); 

但我想創建方法採取在一個字符串是這樣的:

method("127.0.0.1",9050); 

public void method (String a){ 
SocketAddress addr = new InetSocketAddress(a); //can't place string here is there away forit work? 
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);  
} 

有沒有什麼辦法讓新InetSocketAddress取字符串IP?

回答

2

你要通過該端口的構造太,而不僅僅是IP地址:

method("127.0.0.1",9050); 

public void method (String a, int port){ 
    SocketAddress addr = new InetSocketAddress(a, port); 
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);  
} 
+0

我只是寫一個... +1。 – syb0rg