Search code examples
javaandroidapigoogle-books

Failed to use Google Books API Library


I am developing an android app that makes use of Google Books. I found the API, registered an API KEY and want to use the library instead of parsing it myself like this. The code comes from sample project from Google. The project can be successfully compiled but it does not work. The "HELLO" is shown but "WORLD" is not.

Info: 1. The API key is correct. 2. Application name is not null. 3. Tried debugging in eclipse and also export to APK with keystore (registered with API key). Both failed to run the code.

Toast.makeText(this,"HELLO", 1).show();

// Set up Books client.
final Books books = new Books.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, null)
        .setApplicationName(APPLICATION_NAME)
        .setGoogleClientRequestInitializer(new BooksRequestInitializer(ClientCredentials.API_KEY))
        .build();
Toast.makeText(this, "WORLD", 1).show();

What is wrong with it?

By the way, is it necessary to export with keystore in order to run the above code and debug?


Solution

  • Solution found:

    1. Use com.google.api.client.extensions.android.http.AndroidHttp.newCompatibleTransport instead of the Java one. Please refer to this.
    2. Use com.google.api.client.extensions.android.json.AndroidJsonFactory
    3. Be careful of android.os.NetworkOnMainThreadException. Read this.