Dear all,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. Forgive me for mixing butter and milk !
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)

public class GoogleQuery {


private final String HTTP_REFERER = "http://www.example.com/";;

public GoogleQuery() {
    makeQuery("questio verum");
    makeQuery("info:http://frankmccown.blogspot.com/";);
    makeQuery("site:frankmccown.blogspot.com");
}

private void makeQuery(String query) {

    System.out.println("\nQuerying for " + query);

try{
    // Convert spaces to +, etc. to make a valid URL
    query = URLEncoder.encode(query, "UTF-8");
    String url_="the url";
    URL url = new URL(url_);
    URLConnection connection = url.openConnection();
    connection.addRequestProperty("Referer", HTTP_REFERER);

    // Get the JSON response
    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));
        while((line = reader.readLine()) != null) {
            builder.append(line);
    }

    String response = builder.toString();
    JSONObject json = new JSONObject(response);

    System.out.println("Total results = "
+json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount"));

    JSONArray ja =
json.getJSONObject("responseData").getJSONArray("results");
    System.out.println("\nResults:");

    for (int i = 0; i < ja.length(); i++) {
        System.out.print((i+1) + ". ");
        JSONObject j = ja.getJSONObject(i);
        System.out.println(j.getString("titleNoFormatting"));
        System.out.println(j.getString("url"));
    }
}
catch (Exception e) {
    System.err.println("Something went wrong...");
    e.printStackTrace();
}
}

public static void main(String args[]) {
new GoogleQuery();`
}
}
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 1
thanks

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to