English | Site Directory

Google Code Search Data API

Google Code Search Data API Developer's Guide

The Google Code Search data API allows client applications to search public source code for function definitions and sample code.

Contents

Audience

This document is intended for programmers who want to write client applications that can interact with Code Search. It provides a series of examples of basic data API interactions, with explanations.

For Code Search data API reference information, see the reference guide.

This document assumes that you understand the general ideas behind the Google data APIs protocol. It also assumes that you know how to program in Java.

The examples in this document use the GData Java client library; see the client library documentation for more information and more (non-Code-Search-specific) examples. If your client is written in another language, see the client library documentation for that language, if available; you can translate the examples in this document into other languages as needed.

For more information about Google Code Search, see the Google Code Search FAQ.

Interacting with Google Code Search: examples

To send a query to Google Code Search, your client application can take either of the following approaches:

  1. Send a query manually
  2. Send a query using the client library

Send a query manually

Code Search lets you request all known public code samples that match a specified regular expression. For details about query parameters, see Google Code Search query parameters reference.

To get a feed of results from Code Search, send an HTTP GET request to http://www.google.com/codesearch/feeds/search, with the appropriate query parameters.

For example, to request a feed of occurrences of the string malloc, you can use the following HTTP request:

GET http://www.google.com/codesearch/feeds/search?q=malloc

When you send that request, Code Search returns an HTTP 200 OK status code and a feed containing entries representing the search results.

An entry in the returned feed might resemble the following:

<entry xmlns:gcs="http://schemas.google.com/codesearch/2006">
  <id>http://www.google.com/codesearch?q=malloc+show:fLwrFGa3hx</id>
  <updated>2006-10-02T15:08:42Z</updated>
  <author>
    <name>Code owned by external author.</name>
  </author>
  <title type="text">w3c-libwww-5.4.0/Library/src/wwwsys.h</title>
  <link rel="alternate" type="text/html"
      href="http://www.google.com/codesearch?q=malloc+show:fLwrFGa3hxs"/>
  <gcs:package name="http://www.w3.org/Library/Distribution/w3c-libwww-5.4.0.zip"
      uri="http://www.w3.org/Library/Distribution/w3c-libwww-5.4.0.zip"/>
  <gcs:file name="w3c-libwww-5.4.0/Library/src/wwwsys.h"/>
  <gcs:match lineNumber="706" type="text/html">
    &lt;pre&gt;Memory Module for how to handle &lt;b&gt;malloc&lt;/b&gt; and &lt;/pre&gt;
  </gcs:match>
  <gcs:match lineNumber="716" type="text/html">
    &lt;pre&gt;#define &lt;b&gt;malloc&lt;/b&gt;	VAXC$&lt;b&gt;MALLOC&lt;/b&gt;_OPT&lt;/pre&gt;
  </gcs:match>
  <gcs:match lineNumber="1032" type="text/html">
    &lt;pre&gt;/* &lt;b&gt;malloc&lt;/b&gt;.h */&lt;/pre&gt;
  </gcs:match>
  <gcs:match lineNumber="1033" type="text/html">
    &lt;pre&gt;#ifdef HAVE_&lt;b&gt;MALLOC</b&gt;_H&lt;/pre&gt;
  </gcs:match>
  <gcs:match lineNumber="1034" type="text/html">
    &lt;pre&gt;#include &lt;&lt;b&gt;malloc&lt;/b&gt;.h&gt;&lt;/pre&gt;
  </gcs:match>
</entry>

For information about the elements used in an entry, see Google Code Search elements reference.

Send a query using the client library

You can use the client library to send a query instead of sending it by hand.

For example, to search for occurrences of the string malloc, use the following Java code:

CodeSearchService myService = new CodeSearchService("exampleCo-exampleApp-1");
URL feedUrl = new URL("http://www.google.com/codesearch/feeds/search?q=malloc");

// Send the request and receive the response:
CodeSearchFeed resultFeed =
  myService.getFeed(feedUrl, CodeSearchFeed.class);
System.out.println("Number of Entries Received: " + resultFeed.getEntries().size());

The CodeSearchService class represents a client connection to a GData service. When you pass the complete query URL to the CodeSearchService.getFeed method, the client library sends your query and receives and parses the returned XML.

For more information and examples, see the Java client library documentation.

Back to top