0

有没有办法将字符串参数传递给 的参数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?

4

1 回答 1

3

您还必须将端口传递给构造函数,而不仅仅是 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);    
}  
于 2013-04-02T23:40:11.397 回答