java - not able to generate random no to access random api -
i'm trying build android app provides result based on users previous search history, i'm using google search api search result , reranking result based on history. unfortunately google search api gives 100 searches free generated 3 different api's using different id's when i'm using random no generator does't work. here code snippet
package com.aas.pase; import java.util.*; import java.io.ioexception; import java.net.urlencoder; import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.random; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import com.aas.pase.r; import com.aas.pase_db.link; import com.aas.pase_db.manipulatedb; import android.app.activity; import android.app.progressdialog; import android.content.dialoginterface; import android.content.dialoginterface.oncancellistener; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.text.html; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.edittext; import android.widget.listview; import android.widget.textview; public class resultactivity extends activity { arraylist<link> result = new arraylist<link>(); manipulatedb manidb; edittext txtkword; listview lvresult; resultadapter ada = new resultadapter(); textview title; textview link; textview snippet; int totalresults = 99; // network variables... httpclient cli; httpget post; httpresponse res; httpentity resent; string keyword; string key; string strresult; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.result); manidb = new manipulatedb(this); keyword = this.getintent().getextras().getstring("keyword"); txtkword = (edittext) this.findviewbyid(r.id.txtkeyword); txtkword.settext(keyword); txtkword.setselection(keyword.length()); lvresult = (listview) this.findviewbyid(r.id.lvresults); lvresult.setadapter(ada); fetchresulttask task = new fetchresulttask(); task.execute(); } public void onsearch(view v) { keyword = txtkword.gettext().tostring(); fetchresulttask task = new fetchresulttask(); task.execute(); } public class fetchresulttask extends asynctask<void, integer, boolean> { progressdialog pd; private random generator; @override protected void onpreexecute() { // todo auto-generated method stub result.clear(); pd = new progressdialog(resultactivity.this); pd.setmessage("searching web..."); pd.setprogressstyle(progressdialog.style_spinner); pd.setoncancellistener(new oncancellistener(){ @override public void oncancel(dialoginterface arg0) { // todo auto-generated method stub cancel(false); } }); pd.show(); } @override protected boolean doinbackground(void... arg0) { // todo auto-generated method stub try{ cli = new defaulthttpclient(); keyword = urlencoder.encode(keyword); key="aizasycmkn7vrofn66vknauvgrhjhn8qbv6kmhk"; /*random rand=new random(); switch(rand.nextint(3)+1){ case 1: key="aizasycmkn7vrofn66vknauvgrhjhn8qbv6kmhk"; break; case 2: key="aizasydcjjrufniqhseakigachu0hwzf6s-n1e0"; break; case 3: key="aizasyajtx5sqjrmkd9il9fbmbkhsip0ld5n1na"; }*/ post = new httpget("https://www.googleapis.com/customsearch/v1?q=" + keyword + "&"+ key +"&cx=001661526915081556349:glrhiznip8g&atl=json"); res = cli.execute(post); resent = res.getentity(); strresult = entityutils.tostring(resent, "utf-8"); // getting json object link object jsonobject main = new jsonobject(strresult); jsonobject queries = main.getjsonobject("queries"); jsonarray nextpage = queries.getjsonarray("request"); int total = integer.parseint(nextpage.getjsonobject(0).getstring("totalresults")); if(total < 100 ) totalresults = total; if(totalresults > 0) { jsonarray items = main.getjsonarray("items"); for(int = 0; < items.length(); i++) { jsonobject entry = items.getjsonobject(i); link temp = new link(); temp.sethtmlsnippet(entry.getstring("htmlsnippet")); temp.sethtmltitle(entry.getstring("htmltitle")); temp.sethtmlurl(entry.getstring("htmlformattedurl")); temp.setlink(entry.getstring("link")); temp.settitle(entry.getstring("title")); result.add(temp); } this.publishprogress(1); // reading results 11 100... int loopcount = totalresults / 10; if((totalresults % 10) == 0) loopcount--; int curstart = 1; for(int j = 1; j <= loopcount; j++) { curstart += 10; post = new httpget("https://www.googleapis.com/customsearch/v1?q=" + keyword + "&"+ key +"&cx=001661526915081556349:glrhiznip8g&atl=json&start=" + curstart); res = cli.execute(post); resent = res.getentity(); strresult = entityutils.tostring(resent, "utf-8"); jsonobject inmain = new jsonobject(strresult); jsonarray initems = inmain.getjsonarray("items"); for(int = 0; < initems.length(); i++) { jsonobject entry = initems.getjsonobject(i); link temp = new link(); temp.sethtmlsnippet(entry.getstring("htmlsnippet")); temp.sethtmltitle(entry.getstring("htmltitle")); temp.sethtmlurl(entry.getstring("htmlformattedurl")); temp.setlink(entry.getstring("link")); temp.settitle(entry.getstring("title")); result.add(temp); } } this.publishprogress(2); // assigning clicks sorting results... for(link tmp : result) tmp.setclicks(manidb.getclicks(tmp.getlink())); // soring... collections.sort(result, new comparator<link>(){ @override public int compare(link one, link two) { // todo auto-generated method stub return integer.signum(two.getclicks() - one.getclicks()); } }); return true; } return false; } catch(ioexception e){ e.printstacktrace(); } catch(illegalstateexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return false; } @override protected void onprogressupdate(integer... values) { // todo auto-generated method stub switch(values[0]) { case 1: pd.setmessage("reading results"); break; case 2: pd.setmessage("arranging results"); break; } } public string getpageno(int startindex) { int n = startindex; int sum = 0; while (n > 0) { int p = n % 10; sum = sum + p; n = n / 10; } return integer.tostring(sum); } @override protected void onpostexecute(boolean isok) { // todo auto-generated method stub if(!iscancelled()) { pd.dismiss(); log.v("mani", "total: " + result.size()); if(isok) { ada.notifydatasetchanged(); lvresult.setselectionafterheaderview(); } else commons.showtoast("unable results", true); } } } public class resultadapter extends baseadapter { @override public int getcount() { // todo auto-generated method stub return result.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public view getview(final int pos, view view, viewgroup vgrp) { // todo auto-generated method stub if(view == null) view = getlayoutinflater().inflate(r.layout.result_item, null); // adding results list... title = (textview) view.findviewbyid(r.id.txtitemtitle); link = (textview) view.findviewbyid(r.id.txtitemlink); snippet = (textview) view.findviewbyid(r.id.txtitemsnippet); link temp = result.get(pos); title.settext(html.fromhtml(temp.gethtmltitle())); link.settext(html.fromhtml(temp.gethtmlurl())); snippet.settext(html.fromhtml(temp.gethtmlsnippet())); title.setonclicklistener(new onclicklistener(){ @override public void onclick(view view) { // todo auto-generated method stub link tmplink = result.get(pos); manidb.addclick(tmplink); intent browse = new intent(intent.action_view, uri.parse(tmplink.getlink())); startactivity(browse); } }); return view; } }
}
Comments
Post a Comment