Google Product Forums

Re: Transfer ownership of your Site Search engine!

SANOGO Dec 21, 2011 4:10 AM
Posted in group: Google Custom Search

Categories: Creating and Editing :


SANOGO
Level 1
12/19/11
Edit
 
Dear all,Forgive me for mixing butter and milk !
This is my first time posting on google in general and this post may not be related to above topic but trust me, I am writing it out of desperacy. I could NOT get anywhere suitable to post it and I need your help. 
my Custom search url : https://www.googleapis.com/customsearch/v1?key=MY-KEY-HERE&cx=LONG-NUM:CX-ID&q=Latest from NASA&alt=json is working perfectly on the browser but invoking custom search from java code(all of the below codes are throwing the same exception: java.net.ConnectException: Connection refused: connect).I know I have valide key and CX values and google API explorer proved me right as well as any browser. here are a few snipet that all throw the same execption: 
(1)

public void method1(){

        String search = "NASA latest research";
        String charset = "UTF-8";

        try{

        //Reader reader = new InputStreamReader(new URL(the url).openStream());
       // Reader reader = new InputStreamReader(new URL(google).openStream(), charset);
        Reader reader = new InputStreamReader(new URL(google+ URLEncoder.encode(search, charset)).openStream(), charset);


        GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
        //List<com.google.api.services.customsearch.model.Result> result = new Gson().fromJson(reader, List.class);         

        System.out.println(results.getResponseData().getResults().get(0).getTitle());
        System.out.println(results.getResponseData().getResults().get(0).getUrl());
        }catch(MalformedURLException urle){
            urle.printStackTrace();
        }
        catch(UnsupportedEncodingException ence){
            ence.printStackTrace();
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
}
(2) String google = "the url";
public void search0(){

            try{
                GoogleSearchQueryFactory factory = GoogleSearchQueryFactory.newInstance("MY-KEY-HERE");
                WebSearchQuery query = factory.newWebSearchQuery();
                PagedList<WebResult> response = query.withQuery("Latest from NASA").list();
                System.out.println(response.getCurrentPageIndex());
                System.out.println(response.getEstimatedResultCount());
                System.out.println(response.getMoreResultsUrl());
                System.out.println(response.getPages());

                for (WebResult result : response) {
                        System.out.println(result.getTitle());                  
                        System.out.println(result.getContent());                        
                        System.out.println(result.getUrl());                    
                        System.out.println("=======================================");                  
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }and

(3) and a third and fourth approach but to no avail...

None of the above would work and all return the same e. does it mean this can work only via the :com.google.api.services.customsearch.Customsearch. well ,I have tried that too but with compiler errors on these deprecated mthods:

I really need a urgent help.Thanks in Advance !! Really grateful thanks

my email is : kade...@gmail.com