我需要使用我的 android 应用搜索谷歌并返回结果。我尝试使用 Google 自定义搜索 API。它在我桌面上的java中运行良好。但是相同的代码给出了
09-07 02:03:25.101: E/dalvikvm(959): 找不到类 'com.google.api.services.customsearch.Customsearch',从方法中引用
LogCat 中的错误。但是我已经在构建路径中包含了这个类。任何人都可以请提出一种克服这一点的方法。我在下面包含了我的完整代码
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());
}
}
}