连接到互联网:Java应用中的Google自定义搜索“未连接"; () [英] Connect to internet : Google Custom Search in java app "not connecting" ()

查看:115
本文介绍了连接到互联网:Java应用中的Google自定义搜索“未连接"; ()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Java中使用Google自定义搜索api,但它没有连接. 我正在从Google接收json数据.(我应该可以,但它没有连接.. :()

I am starting out on using google custom search api in java but it is just not connecting. I am receiving json data from google.(i am supposed to but it is not connecting..:()

 HttpResponse response = httpClient.execute(httpGet); 
 // exception is thrown at this point.

这是我代码中的一行. 代码是:

which is a line in my code. Code is :

public class MyGoogleSearch{
final static  String searchURL = "https://www.googleapis.com/customsearch/v1?";
// This is Important : 

final static String apiKey = "My-Key-has a '-'";
final static String customSearchEngineKey = "My-Custom-search-engine-that-has a ':' too";

public String makeSearchString(String qSearch){
    String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey;
    toSearch += "&q=" + qSearch + "&alt=json";
    return toSearch;
}

public static void main(String[] argv) throws IOException{
    System.setProperty("proxyHost", "my-host");
    System.setProperty("proxyPort","my-Port");

    MyGoogleSearch browser = new MyGoogleSearch();
    String toSearch = browser.makeSearchString("flower");
    System.out.println(toSearch);
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(toSearch);

    HttpResponse response = httpClient.execute(httpGet);
    // exception is thrown here.
    HttpEntity entity = response.getEntity();

    if(entity != null){
        InputStream inStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        int length = 0;
        byte[] buffer  = new byte[2048];
        while((length = inStream.read(buffer)) != -1)
            System.out.println(buffer);
        String inputLine;
        while((inputLine = reader.readLine()) != null)
            System.out.println(inputLine);
    }
}

}

//错误是:

Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connection to https://www.googleapis.com refused at   
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)

....

Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)

这是有效的方法吗? 我没有做任何形式的UTF-8编码.那会是个问题吗?

Is this the valid way to do ? I have not done any kind of UTF-8 encoding. Would that be a problem ?

http://code.google.com/apis/customsearch/v1/Getting_started.html
这是一个在JSON/Atom自定义搜索API中如何工作的示例,该API在测试的自定义搜索引擎中搜索讲座:

http://code.google.com/apis/customsearch/v1/getting_started.html
Here is an example of how this works in the JSON/Atom Custom Search API, which searches a test Custom Search Engine for lectures:

GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=讲座

//这是仅用于JavaScript还是用于Java

// Is this for javaScript only or for java too

我去了google api资源管理器 https://code.google.com/apis/explorer/

I went to google api explorer https://code.google.com/apis/explorer/

我选择自定义搜索并在程序中填充* q和cx列:

I choose custom search and filled *q and cx column as i filled in my program:

它显示了结果, GET https://www.googleapis.com/customsearch/v1?q=o& ; cx = {MY-Custom-Search-Engine-No}& pp = 1& key = {YOUR_API_KEY}

and it showed results and GET https://www.googleapis.com/customsearch/v1?q=o&cx={MY-Custom-Search-Engine-No}&pp=1&key={YOUR_API_KEY}

,但这一次cx值在':'字符处有%3.

对于键和cse-no中的所有字符,我该怎么做

How would i do that for all the characters in my keys and cse-no

推荐答案

问题似乎与googleapis有关,您确定使用的是正确的代理吗?尝试仅连接到google.com或任何URL即可测试您是否通过了代理,但也无需设置代理即可尝试.

the problem seems to be connecting to googleapis, are you sure you're using the correct proxy? Try just connecting to google.com or any URL to test you are getting past the proxy, try it without setting the proxy as well.

这篇关于连接到互联网:Java应用中的Google自定义搜索“未连接"; ()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆