0

I am trying to implement http://codify.freebaseapps.com/?request=https%3A%2F%2Fwww.googleapis.com%2Ffreebase%2Fv1%2Fsearch%3Fquery%3DBlue%2BBottle&title=Simple%20Search inside an android application. I have the correct api key installed and matched to the google api service and have imported the appropriate jar files under Referenced Libraries.

My code however keeps throwing a could not find class - 'com.google.api.client.http.javanet.NetHttpTransport' error every time it is run on the emulator. Any suggestions or feedback ?

laser21
  • 1
  • 1

2 Answers2

0

You must add library into project.

  1. right click project
  2. Properties
  3. Java Build Path
  4. Add External JARs

please read this post: Android and Google client API NetHttptransport Class not found

Community
  • 1
  • 1
Melih Mucuk
  • 6,988
  • 6
  • 37
  • 56
  • I have already added the relevant jars to the Referenced Library section as per the process you describe above. Am still confronted with could not find - ''com.google.api.client.http.javanet.NetHttpTransport' – laser21 Feb 08 '13 at 16:16
  • If you go to Android Dependencies in the Package Explorer and expand it to show the google-http-client jar file that you've added you should be able to expand that again to see the com.google.api.client.http.javanet package. If you can see that then you should have access to NetHttpTransport. – Shawn Simister Feb 08 '13 at 18:41
0

When I built the Codify app that you linked to I didn't test it against Android so there may be an easier way to do it in Android.

Here's another way to do it using Apache HttpClient and json.org which are included in the Android SDK.

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

public class FreebaseSearchTask extends AsyncTask<String, Void, JSONObject> {

    protected JSONObject getJsonContentFromEntity(HttpEntity entity)
            throws IllegalStateException, IOException, JSONException {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n > 0) {
            byte[] b = new byte[4096];
            n = in.read(b);
            if (n > 0)
                out.append(new String(b, 0, n));
        }
        JSONObject jObject = new JSONObject(out.toString());
        return jObject;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        String query = params[0];       
        JSONObject result = null;
        try {
            HttpGet httpGet = new HttpGet("https://www.googleapis.com/freebase/v1/search?query=" + URLEncoder.encode(query, "utf-8"));

            HttpResponse response = httpClient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            result = getJsonContentFromEntity(entity);
        } catch (Exception e) {
            Log.e("error", e.getLocalizedMessage());
        }
        return result;
    }

    protected void onPostExecute(JSONObject result) {
        doSomething(result);
    }
}
Shawn Simister
  • 4,613
  • 1
  • 26
  • 31