2013-02-08 100 views
0

我正試圖在android應用程序中實現http://codify.freebaseapps.com/?request=https%3A%2F%2Fwww.googleapis.com%2Ffreebase%2Fv1%2Fsearch%3Fquery%3DBlue%2BBottle&title=Simple%20Search。我安裝了正確的API密鑰並與google api服務相匹配,並在Referenced Libraries下導入了相應的jar文件。無法在Android應用程序中找到com.google.api.client.htpp.javanet.NetHttpTransport在Android應用程序中

但是,我的代碼一直在拋出一個找不到類 - 每次在模擬器上運行時出現'com.google.api.client.http.javanet.NetHttpTransport'錯誤。任何建議或反饋?

回答

0

您必須將庫添加到項目中。

  1. 右擊項目
  2. 屬性
  3. Java構建路徑
  4. 添加外部JAR

請閱讀這篇文章:Android and Google client API NetHttptransport Class not found

+0

我已經按照上述過程將相關的jar添加到引用庫部分。我仍然遇到找不到 - ''com.google.api.client.http.javanet.NetHttpTransport' – laser21 2013-02-08 16:16:32

+0

如果你去Package Explorer中的Android Dependencies並展開它來顯示你的google-http-client jar文件已添加您應該可以再次展開以查看com.google.api.client.http.javanet包。如果你能看到,那麼你應該有權訪問NetHttpTransport。 – 2013-02-08 18:41:37

0

當我建你鏈接的編纂程序因爲我沒有對Android進行測試,因此在Android中可能會有更簡單的方法。

下面是使用Android SDK中包含的Apache HttpClient和json.org完成此操作的另一種方法。

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); 
    } 
} 
相關問題