json - Google custom search api in android application -


I am trying to use Google Custom Search API for a school work and when I am trying to get results Get Jason's look, I've configured everything on the Google Developer Console and Google Custom Search Engine Control Panel, at least I think my code snippet here is qstring = " Key = "+ key +" & amp; cx = "+ cx +" & amp; q = "+ searchtext +" and alt = json "+" & start = "+" 0 "; String query = null; Try {query = URLEncoder.encode (qstring, "UTF-8") .replaceAll ("\\% 28", "(") .replaceAll ("\\% 29", ")") .replaceAll ("\\ "ReplaceAll" ("\\% 27", "'"). (Unsupported encoding exception e) {// TODO auto generated blocking block e.printStackTrace ();} URL url = null; {Url = new URL ("https://www.googleapis.com/customsearch/v1?" + Query);} hold (malformedlexception e) {// TODO auto generated blocking block e.printStackTrace ();} HttpURL connection conn2 = tap; try {conn2 = (HttpURLConnection) url.openConnection ();} hold (IOException e ) {// TODO Auto-Generated Cache Block e.printStackTrace ();} {Conn2.setRequestMethod ("GET") Priya (Protocol exception e) {// Todo auto-generated catch block e.printStackTrace ();} Conn2.setRequestProperty ("Accept", "App / JS"); Try {BufferedReader br = New BufferedReader (New InputStreamReader ((conn2.getInputStream ())); string line; while ((line = br.readline ())! = Null ({content.append (line + "\ n");} Br.close (); return content.toString ();} hold (IOException e) {// TODO auto-generated cache block e.printStackTrace () ;}

and the error appearing in the logcat

  04-22 07: 26: 36.253: w / system. Er (4229): java Io.FileNotFoundException: https://www.googleapis.com/customsearch/v1?key%3DAIzaSyBgvvgYg3mYS66fMM9j0qpaG6wlvUc1KLk%26cx%3D008838294879486691568%3Axy_pvqrl6fa%26q%3Djava.pdf%26alt%3Djson%26start%3D0 04-22 07: 26: 36.289: W / System. Er (4229): at libcore.net.http.httpURLConnectionImpl.getInputStream (HttpURLConnectionImpl.java:186) 04-22 07: 26: 36.321: W / System. Er (4229): on libcore.net .http.httpsURLConnectionImpl.getInputStream (Http SURLConnectionImpl.javaailand71)   

I appreciate a point in the right direction, I am doing something wrong. Please help!

< Div class = "post-text" itemprop = "text">

I'm not sure whether this will help your situation, but I had a lot of trouble with my search string unless I do not know How to properly encode it I noticed that you are doing manual insertion of many codes (eg,% 20) and the methods of the object To work it might be more reliable. I'm not working in Java, but here's how I did it in C #. I encoded APIKey, CSKey and query separately and added all three to the base Google search string. Then I finally added "& alt = json". The string variable holds the returned string in 'Result'.

  string myQuery = "action inspiration, Inc."; String end = string Format ("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}", HttpUtility.UrlEncode (APIKey), HttpUtility.UrlEncode (CXKey ), HttpUtility.UrlEncode (myQuery)); Last + = "& amp; alt = json"; WebRequest myRequest = WebRequest.Create (last); HttpWebResponse myResponse = (HttpWebResponse) myRequest.GetResponse (); Stream mystream = myResponse.GetResponseStream (); Streamminder Myrard = New Stream Reader (myResponse.GetResponsestream (), System.Text.Encoding.UTF8); String result = myReader.ReadToEnd ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -