android - how to check if url exist or not to fetch json data -


i fetching json data different book_name & working fine.but in case if book not exists app collapse.i know jsonobject fetching null data want rather collapse when book not found. code is: `

   public class asynctaskparsejson extends asynctask<string, string, string> {      final string tag = "asynctaskparsejson.java";      // set json string url here    intent po=getintent();     string bok=po.getstringextra("bookname");     string newname = bok.replaceall("\\s", "%20");     string yourjsonstringurl = "http://www.bsservicess.com/photoupload/star_avg.php?bookname="+newname;      // contacts jsonarray     jsonarray datajsonarr = null;     @override     protected void onpreexecute() {}       protected string doinbackground(string... arg0) {         if(exists(yourjsonstringurl)) {         try {             jsonparser jparser = new jsonparser();              // json string url              jsonobject json = jparser.getjsonfromurl(yourjsonstringurl);                    // array of users                 datajsonarr = json.getjsonarray("result");                 jsonobject c = datajsonarr.getjsonobject(0);                 //    na=c.getstring("avg");                  ratenums = c.getstring("avg");                 numbers = c.getstring("num");                 starts = float.parsefloat(c.getstring("avg"));           } catch (jsonexception e) {             e.printstacktrace();         }         }else{             starts=0;             numbers="0";         }          return null;     }      @override     protected void onpostexecute(string strfromdoinbg) {         super.onpostexecute(strfromdoinbg);         ratenum.settext(ratenums);         number.settext("("+numbers+")");         netrate.setrating(starts);      //   toast.maketext(mybookview.this, float.tostring(starts),toast.length_short).show();     } 

plz me. `

       public jsonobject getjsonfromurl(string url) {      // make http request     try {          defaulthttpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url);          httpresponse httpresponse = httpclient.execute(httppost);         httpentity httpentity = httpresponse.getentity();         = httpentity.getcontent();      } catch (unsupportedencodingexception e) {         e.printstacktrace();     } catch (clientprotocolexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }      try {          bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8);         stringbuilder sb = new stringbuilder();         string line = null;         while ((line = reader.readline()) != null) {             sb.append(line + "\n");         }         is.close();         json = sb.tostring();      } catch (exception e) {         log.e(tag, "error converting result " + e.tostring());     }      // try parse string json object     try {         jobj = new jsonobject(json);     } catch (jsonexception e) {         log.e(tag, "error parsing data " + e.tostring());     }      // return json string     return jobj; } 

}`

// json string url  jsonobject json = jparser.getjsonfromurl(yourjsonstringurl); if(json == null){     //url parsing didn't work     return; } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -