traveller traveller - 1 month ago 12
Android Question

StringIndexOutOfBoundsException using Volley library

This is my code:

private void bringData() {
final TextView mTextView = (TextView) findViewById(R.id.textView);

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.4.1:8080/";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}


It's the default given from the android documentation. I only changed the url.

This is my error message:


java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
regionLength=499
at java.lang.String.substring(String.java:1931)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50)
at com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


During debugging I see that at
mTextView.setText("Response is: "+ response.substring(0,500));
my message is delivered to me but the textview is never updated and the app crashes.

Specifically, it crashes here inside the Looper.Java file:

finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}


traceTag is 0.

I read that some string bounds are wrong but I cannot find out how to fix it.

Answer

See it in this way

java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1; 
regionLength=499 at java.lang.String.substring(String.java:1931) at 

mean

java.lang.StringIndexOutOfBoundsException: String length is not long enough      
as the string length =28  and you are trying to create a substring with  
regionStart(beg)=1; regionLength(end)=499 
so this is not possible 

Solution :

  mTextView.setText("Response is: "+ ((response.length()>499)?response.substring(0,500):"whatever you want to display"));

or

String msg="wrong response";
if(response.length()>499){
    msg=response.substring(0,500);
}
mTextView.setText("Response is: "+msg);

//or     mTextView.setText("Response is: "+response);
Comments