Android code for using google custom search API -


can please share java codes getting response google search. stuck on "result=403" , "connect failed:network unreachable".please check coding below. i'll thankful if can me.(i have obtained api key , custom search engine id).

thanks.

public class mainactivity extends appcompatactivity {     private string searchurl = "https://www.googleapis.com/customsearch/v1?key=aizasybypqz3jzg5kosg7emxsgthhp3sil7dyik&cx=012845902157700116531:oqktayhycq4&q=";     private string searchitem = "android";     private string searchquery = searchurl + searchitem + "&alt=json";     textview searchresult;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          searchresult = (textview) findviewbyid(r.id.result);         new jsonsearchtask().execute();     }      private class jsonsearchtask extends asynctask<void, void, void> {         string searchresultstring = "";          @override         protected void doinbackground(void... params) {             try {                 searchresultstring = parsestringresult (sendquery(searchquery));             } catch (exception e) {                   e.printstacktrace();             }             return null;         }      @override     protected void onpostexecute(void avoid) {         searchresult.settext(searchresultstring);         super.onpostexecute(avoid);     }  }        private string sendquery(string query) throws ioexception{            string result="";            url surl=new url(query);            httpurlconnection httpurlconnection=(httpurlconnection) surl.openconnection();            httpurlconnection.setrequestmethod("get");            httpurlconnection.setrequestproperty("accept", "application/json");            //if(httpurlconnection.getresponsecode()==httpurlconnection.http_ok){                inputstreamreader inputstream=new inputstreamreader(httpurlconnection.getinputstream());                bufferedreader bufferedreader=new bufferedreader(inputstream,8192);                string line=null;                while((line=bufferedreader.readline())!=null){                    result+=line;                }                   bufferedreader.close();          //  }        return result;    }     private string parsestringresult(string json) throws jsonexception{         string parseresult="";          jsonobject jsonobject= new jsonobject(json);         jsonobject jsonobject_responsedata=jsonobject.getjsonobject("responsedata");         jsonarray jsonarray_result=jsonobject_responsedata.getjsonarray("results");          parseresult+="google search :"+searchitem+"\n";         parseresult+="number of result returned ="+jsonarray_result.length()+"\n\n";          for(int i=0;i<jsonarray_result.length();i++){             jsonobject jsonobject_i=jsonarray_result.getjsonobject(i);             parseresult+="title"+jsonobject_i.getstring("title")+"\n";             parseresult+="content"+jsonobject_i.getstring("content")+"\n";             parseresult+="url"+jsonobject_i.getstring("url")+"\n\n";          }       return parseresult;     } } 


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -