我需要搜索谷歌并使用我的Android应用程序获取结果。我尝试使用Google自定义搜索API。它在我的桌面上的java工作正常。但是相同的代码给出了
LogCat中的09-07 02:03:25.101:E / dalvikvm(959):找不到从方法
引用的课程'com.google.api.services.customsearch.Customsearch'
错误。但我已将该类包含在构建路径中。任何人都可以建议一种克服这一点的方法。我已将我的完整代码包括在下面
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());
}
}
}
答案 0 :(得分:4)
您必须包含