android - java.lang.StringIndexOutOfBoundsException: -


i'm new in android , want send edittext value using json server . error "stringindexoutofboundsexception" , don't know how can fix it. here code :

jsonparser.java

package com.example.bookstore;   public class jsonparser  {  static inputstream = null; static jsonobject jobj = null; static string json = "";  // constructor public jsonparser() {  }  // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method,         list<namevaluepair> params) {      // making http request     try {          // check request method         if(method.equals("post")){              httpclient httpclient = getnewhttpclient();             httppost httppost = new httppost(url);             httppost.setentity(new urlencodedformentity(params));              httpresponse httpresponse = httpclient.execute(httppost);             if(httpresponse != null){             httpentity httpentity = httpresponse.getentity();             = httpentity.getcontent();             }          }else if(method.equals("get")){             // request method             httpclient httpclient = getnewhttpclient();             string paramstring = urlencodedutils.format(params, "utf-8");             url += "?" + paramstring;             httpget httpget = new httpget(url);              httpresponse httpresponse = httpclient.execute(httpget);             if(httpresponse != null){             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;         if(reader != null){         while ((line = reader.readline()) != null) {             sb.append(line + "\n");         }         }         is.close();         json = sb.tostring();     } catch (exception e) {         log.e("buffer error", "error converting result " + e.tostring());     }      // try parse string json object     try {         jobj = new jsonobject(json.substring(json.indexof("{"), json.lastindexof("}") + 1));     } catch (jsonexception e) {         log.e("json parser", "error parsing data " + e.tostring());     }      // return json string     return jobj;  }   public httpclient getnewhttpclient() {     try {         keystore truststore = keystore.getinstance(keystore.getdefaulttype());         truststore.load(null, null);          sslsocketfactory sf = new mysslsocketfactory(truststore);         sf.sethostnameverifier(sslsocketfactory.allow_all_hostname_verifier);          httpparams params = new basichttpparams();         httpprotocolparams.setversion(params, httpversion.http_1_1);         httpprotocolparams.setcontentcharset(params, http.utf_8);          schemeregistry registry = new schemeregistry();         registry.register(new scheme("http", plainsocketfactory.getsocketfactory(), 80));         registry.register(new scheme("https", sf, 443));          clientconnectionmanager ccm = new threadsafeclientconnmanager(params, registry);          return new defaulthttpclient(ccm, params);     } catch (exception e) {         return new defaulthttpclient();     }   }  } 

sale.java

package com.example.bookstore;  public class sale extends activity {  // progress dialog private progressdialog pdialog;  jsonparser jsonparser = new jsonparser(); edittext bookname; edittext authorname; edittext publication; edittext price; edittext bookgenre;  // url create new product private static string url_create_product = "https://5.144.130.36/android/create_product.php";  // json node names private static final string tag_success = "success";  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build();     strictmode.setthreadpolicy(policy);     setcontentview(r.layout.sale);      bookname = (edittext) findviewbyid(r.id.bookname);     authorname = (edittext) findviewbyid(r.id.authorname);     publication = (edittext) findviewbyid(r.id.publication);     price = (edittext) findviewbyid(r.id.price);     bookgenre = (edittext) findviewbyid(r.id.bookgenre);       button confirm = (button) findviewbyid(r.id.confirm);       confirm.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view view) {             // creating new product in background thread             new createnewbook().execute();         }     }); }  class createnewbook extends asynctask<string, string, string> {      @override     protected void onpreexecute() {         super.onpreexecute();         pdialog = new progressdialog(sale.this);         pdialog.setmessage("book submition");         pdialog.setindeterminate(false);         pdialog.setcancelable(true);         pdialog.show();     }      protected string doinbackground(string... args) {           runonuithread(new runnable() {                 public void run() {         string b_name = bookname.gettext().tostring();         string au_name = authorname.gettext().tostring();         string pub = publication.gettext().tostring();         string pr = price.gettext().tostring();         string b_genre = bookgenre.gettext().tostring();           list<namevaluepair> params = new arraylist<namevaluepair>();         params.add(new basicnamevaluepair("b_name", b_name));         params.add(new basicnamevaluepair("au_name", au_name));         params.add(new basicnamevaluepair("pub", pub));         params.add(new basicnamevaluepair("pr", pr));         params.add(new basicnamevaluepair("b_genre", b_genre));          // getting json object         // note create product url accepts post method         jsonobject json = jsonparser.makehttprequest(url_create_product,                 "post", params);          // check log cat fro response         log.d("create response", json.tostring());          // check success tag         try {             int success = json.getint(tag_success);              if (success == 1) {                 intent = new intent(getapplicationcontext(), mainactivity.class);                 startactivity(i);                 finish();             }          } catch (jsonexception e) {             e.printstacktrace();         }                  }      });          return null;     }      /**      * after completing background task dismiss progress dialog      * **/     protected void onpostexecute(string result) {         // dismiss dialog once done         pdialog.dismiss();     }  } public void ondestroy() {     super.ondestroy();     if (pdialog != null) {         pdialog.dismiss();         pdialog = null;     } }  } 

logcat

04-29 11:13:59.536: e/androidruntime(784):   java.lang.stringindexoutofboundsexception: length=58; regionstart=-1;  regionlength=1 04-29 11:13:59.536: e/androidruntime(784):  @    java.lang.string.startendandlength(string.java:583) 04-29 11:13:59.536: e/androidruntime(784):  @    java.lang.string.substring(string.java:1464) 04-29 11:13:59.536: e/androidruntime(784):  @    com.example.bookstore.jsonparser.makehttprequest(jsonparser.java:109) 

i have mysslsocketfactory class certification don't think problem exists in there.

jobj = new jsonobject(json.substring(json.indexof("{"), json.lastindexof("}") + 1)); 

why add 1 last index? if receive string last } on last index, have outofbounds.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -