我正在尝试实施谷歌自定义搜索API。但我得到以下例外。我不知道为什么?任何人都可以帮助谢谢。
到目前为止我尝试了什么
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
String key = getResources().getString(R.string.search_key);
String qry = "Pdf";
URL url = null;
url = new URL("https://www.googleapis.com/customsearch/v1?q=" + qry + "&cx=009509498206542289642:aac_kxxh3ao&fileType=pdf&key"+key+ "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
if (output.contains("\"link\": \"")) {
String link = output.substring(output.indexOf("\"link\": \"") + ("\"link\": \"").length(), output.indexOf("\","));
System.out.println(link); //Will print the google search links
}
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
例外:
04-14 02:26:23.426 24867-25162/com.pdgproject W/System.err: java.io.FileNotFoundException: https://www.googleapis.com/customsearch/v1?q=Pdf&cx=009509498206542289642:aac_kxxh3ao&fileType=pdf&keyAIzaSyCOnVh7xMKxQ1yBmYZnY4i0wxQBMJ8vNuE&alt=json
04-14 02:26:23.430 24867-25162/com.pdgproject W/System.err: at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
04-14 02:26:23.433 24867-25162/com.pdgproject W/System.err: at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
04-14 02:26:23.446 24867-25162/com.pdgproject W/System.err: at com.pdgproject.MainActivity$3.doInBackground(MainActivity.java:221)
04-14 02:26:23.450 24867-25162/com.pdgproject W/System.err: at com.pdgproject.MainActivity$3.doInBackground(MainActivity.java:208)
04-14 02:26:23.450 24867-25162/com.pdgproject W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-14 02:26:23.456 24867-25162/com.pdgproject W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-14 02:26:23.460 24867-25162/com.pdgproject W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-14 02:26:23.460 24867-25162/com.pdgproject W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-14 02:26:23.463 24867-25162/com.pdgproject W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-14 02:26:23.463 24867-25162/com.pdgproject W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-14 02:26:23.466 24867-25162/com.pdgproject W/System.err: at java.lang.Thread.run(Thread.java:856)
第221行代码
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));