HTTP request with volley Java/Android -
i'm trying make http request page, application aways crash. first java application i'm beginner. been researching while not find solution:
package com.lookingunique.splashstockcontrol; import android.app.activity; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest; import com.android.volley.toolbox.volley; public class mainactivity extends activity { edittext barcode; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); barcode = (edittext) findviewbyid(r.id.etbarcode); final button addbtn = (button) findviewbyid(r.id.button); addbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string barcodeval = barcode.gettext().tostring(); // instantiate requestqueue. requestqueue queue = volley.newrequestqueue(getapplicationcontext()); string url ="http://xxzx.com/zzz/xzxx/yyy.php?barcode="+ barcodeval +"&action=check"; // request string response provided url. stringrequest stringrequest = new stringrequest( request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { // display first 500 characters of response string. string test = response.substring(0,100); toast.maketext(getapplicationcontext(), test, toast.length_short).show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { string test = "that didn't work!"; toast.maketext(getapplicationcontext(), test, toast.length_short).show(); } }); // add request requestqueue. queue.add(stringrequest); } }); barcode.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { addbtn.setenabled(!barcode.gettext().tostring().trim().isempty()); } @override public void aftertextchanged(editable s) { } }); } }
08-25 10:15:15.336 2029-2029/com.lookingunique.splashstockcontrol e/androidruntime﹕ fatal exception: main process: com.lookingunique.splashstockcontrol, pid: 2029 java.lang.stringindexoutofboundsexception: length=0; regionstart=0; regionlength=100 @ java.lang.string.startendandlength(string.java:298) @ java.lang.string.substring(string.java:1087) @ com.lookingunique.splashstockcontrol.mainactivity$1$1.onresponse(mainactivity.java:47) @ com.lookingunique.splashstockcontrol.mainactivity$1$1.onresponse(mainactivity.java:43) @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:60) @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:30) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
it seems request not failing response doesn't contain 100 length string if check documentation substring states that:
throws: indexoutofboundsexception - if beginindex negative, or endindex larger length of string object, or beginindex larger endindex.
try , print string instead
Comments
Post a Comment