针对谷歌Android自定义搜索API,类未找到 [英] Google Custom Search API for android ,class not found
问题描述
我要搜索谷歌和使用我的Android应用程序返回的结果。我尝试使用谷歌定制搜索API。它好工作在Java中我的桌面。但是,同样的code给出
2月9日至7日:03:25.101:E / dalvikvm(959):找不到类的com.google.api.services.customsearch.Customsearch,从方法引用
块引用>错误在LogCat中。但我已经包含在构建路径的类。任何人都可以请建议克服这种的方式。我已经包括以下
我的全code包customSearchAPI.searchTest;进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.List;
进口com.google.api.client.http.javanet.NetHttpTransport;
进口com.google.api.client.json.jackson.JacksonFactory;
进口com.google.api.services.customsearch.Customsearch;
进口com.google.api.services.customsearch.model.Result;
进口com.google.api.services.customsearch.model.Search;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.webkit.WebView;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.TextView;公共类CustomSearchActivity扩展活动实现OnClickListener { / **当第一次创建活动调用。 * /
ListView控件显示;
ArrayList的<串GT;链接=新的ArrayList<串GT;();
按钮搜索按钮;
TextView的搜索框,输出;
ListAdapter适配器;
的WebView网页;
最终静态字符串searchURL =?https://www.googleapis.com/customsearch/v1
// 这个很重要 : 最终静态字符串apiKey =我的API密钥
最终静态字符串customSearchEngineKey =我的CX ID; 公共字符串makeSearchString(字符串qSearch){
字符串toSearch = searchURL +键=+ apiKey +&放大器; CX =+ customSearchEngineKey;
toSearch + =&放大器; Q =+ qSearch +&放大器; ALT = json的;
返回toSearch;
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
搜索框=(的TextView)findViewById(R.id.editText1);
搜寻] =(按钮)findViewById(R.id.button1);
//display=(ListView)findViewById(R.id.listView1);
网页=(的WebView)findViewById(R.id.webView1);
输出=(的TextView)findViewById(R.id.editText2);
} 公共无效的onClick(视图v){
// TODO自动生成方法存根
Customsearch customsearch =新Customsearch(新NetHttpTransport(),新JacksonFactory()); 尝试{
com.google.api.services.customsearch.Customsearch.Cse.List列表= customsearch.cse()列表(searchBox.getText()的toString()。)。
list.setKey(我的API密钥);
list.setCx(我的CX ID);
搜索结果= list.execute();
清单<结果>项= results.getItems();
对于(结果结果:项目)
{
//System.out.println(\"Title:+ result.getHtmlTitle());
links.add(result.getHtmlTitle());
}
webPage.loadUrl(items.get(0).getLink());
display.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,链接)); }赶上(IOException异常五){
// TODO自动生成catch块
searchBox.setText(错误+ e.getMessage());
} }}
解决方案您必须包含
- 不只是构建路径,而是部署了。
- 不仅是一流的,但整个图书馆。
I need to search google and get back results using my android app. I tried using Google custom search API. It worked fine in java in my desktop. But the same code gives
09-07 02:03:25.101: E/dalvikvm(959): Could not find class 'com.google.api.services.customsearch.Customsearch', referenced from method
error in the LogCat. But i have included the class in the build path. Can anyone please suggest a way of overcoming this. I have included my full code below
package customSearchAPI.searchTest; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.customsearch.Customsearch; import com.google.api.services.customsearch.model.Result; import com.google.api.services.customsearch.model.Search; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; public class CustomSearchActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ ListView display; ArrayList<String> links=new ArrayList<String>(); Button searchButton; TextView searchBox,output; ListAdapter adapter; WebView webPage; final static String searchURL = "https://www.googleapis.com/customsearch/v1?"; // This is Important : final static String apiKey = "My API key"; final static String customSearchEngineKey = "My cx id"; public String makeSearchString(String qSearch){ String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey; toSearch += "&q=" + qSearch + "&alt=json"; return toSearch; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); searchBox=(TextView)findViewById(R.id.editText1); searchButton=(Button)findViewById(R.id.button1); //display=(ListView)findViewById(R.id.listView1); webPage=(WebView)findViewById(R.id.webView1); output=(TextView)findViewById(R.id.editText2); } public void onClick(View v) { // TODO Auto-generated method stub Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory()); try { com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list(searchBox.getText().toString()); list.setKey("my api key"); list.setCx("my cx id "); Search results = list.execute(); List<Result> items = results.getItems(); for(Result result:items) { //System.out.println("Title:"+result.getHtmlTitle()); links.add(result.getHtmlTitle()); } webPage.loadUrl(items.get(0).getLink()); display.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,links)); } catch (IOException e) { // TODO Auto-generated catch block searchBox.setText("Error"+e.getMessage()); } } }
解决方案You must include
- not just to build path but to deployment too.
- not only one class but whole library.
这篇关于针对谷歌Android自定义搜索API,类未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!