我正在尝试构建一个 Jersey Rest Client 来使用 Twitter 搜索 api。
代码如下:
package twitter.client.example;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class TwitterClient {
public static void main(String[] args) {
try {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create();
WebResource webResource = client
.resource("http://search.twitter.com/search.json? lang=en&q=pizza");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我运行这个时,我得到一个com.sun.jersey.api.client.ClientHandlerException:
java.net.ConnectException:Connection timed out: connect exception
我在代码中遗漏了什么还是由于代理设置?我需要合并 OAuth 还是搜索 api 仍然未经身份验证?
谢谢, 萨克希