Stephen Herring
Stephen Herring

Reputation: 55

java.lang.NoClassDefFoundError: com.google.api.client.http.javanet.NetHttpTransport

I am trying to use the Google Places API for Android, but whenever I try to run my application I get this error:

04-19 18:27:16.094: E/AndroidRuntime(531): Caused by: java.lang.NoClassDefFoundError: com.google.api.client.http.javanet.NetHttpTransport
04-19 18:27:16.094: E/AndroidRuntime(531):  at com.android.FoodFinder.MainActivity.<clinit>(MainActivity.java:38)

This is my code

package com.android.FoodFinder;


import java.io.IOException;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.jackson.JacksonFactory;

import android.app.ListActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ListActivity
{

    private float distance;
    private Location location;
    private double latitude;
    private double longitude;
    private static String keyString = "0JVolnSwRg8xLH0J6d9rUwQH-e770dlWn3kcRuA";
    private static String urlString = "https://maps.googleapis.com/maps/api/place/search/json?";
    private static final HttpTransport transport = new NetHttpTransport();

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location == null)
        {
            latitude = 0;
            longitude = 0;
        }
        else
        {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }

        final EditText distanceEdit = (EditText)findViewById(R.id.distance);

        final Button confirm = (Button)findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                try
                {
                    distance = Float.parseFloat(distanceEdit.getText().toString());
                    search(distance);                   
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    private void search(float d) throws IOException
    {
        HttpRequestFactory hrfactory = createRequestFactory(transport);
        HttpRequest request = hrfactory.buildGetRequest(new GenericUrl(urlString));
        request.getUrl().put("key", keyString);
        request.getUrl().put("location", latitude + ", " + longitude);
        request.getUrl().put("types", "restaurant");
        request.getUrl().put("radius", distance);
        request.getUrl().put("sensor", "false");

    }

    private static HttpRequestFactory createRequestFactory(final HttpTransport transport)
    {
        return transport.createRequestFactory(new HttpRequestInitializer()
        {

            public void initialize(HttpRequest request) throws IOException 
            {
                request.addParser(new JsonHttpParser(new JacksonFactory()));
            }
        });
    }

}

The error is at:

private static final HttpTransport transport = new NetHttpTransport();

I have added the following jar files to my build path:

But I still manage to get the NoClassDefFoundExeception. What am I doing wrong?

Upvotes: 2

Views: 10145

Answers (2)

Shawn
Shawn

Reputation: 456

I'm using google-http-client-1.11.0 beta.jar (NetHttpTransport located) which is newer than yours, and it works fine, give a try.

Upvotes: 0

Gaurav Agarwal
Gaurav Agarwal

Reputation: 19102

I think the problem is with adding Jars, try this Create a folder at the root name /libs(not /lib) and Project => Properties => Java Build Path => Libraries => Add JAR ...

You can find some more help here and here

Upvotes: 1

Related Questions