1
我需要搜索谷歌,並使用我的Android應用程序取回結果。我嘗試使用Google自定義搜索API。它在我的桌面Java工作正常。但是,同樣的代碼提供谷歌自定義搜索API for android,class not found
2月9日至7日: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());
}
}
}