I have this Runnable
Runnable serverChecksRunnable = new Runnable()
{
@Override
public void run()
{
if (connectedSuccess == true)
{
checkServer = Get(iptouse + "uploadstatus");
}
Handler h=new Handler(Looper.getMainLooper());
h.post(new Runnable()
{
@Override
public void run()
{
if (connectedSuccess)
{
if (checkServer != null)
{
String a = null;
try
{
a = new String(checkServer, "UTF-8");
textforthespeacch = a;
{
String varr = textforthespeacch.substring(17,3);
String varr1 = textforthespeacch.substring(0, 16);
String varr2 = textforthespeacch.substring(21);
textforthespeacch = varr1;
status1.setText("Upload completed" + " " + varr + "%");
timerValue.setText("00:00:" + varr2);
numberofuploadedfilescounter += 1;
uploadedfilescount.setText(("Uploaded Files: " + numberofuploadedfilescounter));
MainActivity.this.initTTS();
}
if (textforthespeacch.contains("uploading"))
{
String[] split = textforthespeacch.split(" ");
textforthespeacch = split[0];
status1.setText("Uploading" + " " + split[1] + "%");
servercheckCounter += 1;
if (servercheckCounter == 1)
{
MainActivity.this.initTTS();
}
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
}
}
});
customHandler.postDelayed(serverChecksRunnable,1000);
}
};
The var textforthespeacch is string and get text from a web server. Then i check when it contains a part of a text assign the text parts to three vars:
if (textforthespeacch.contains("upload completed"))
{
String varr = textforthespeacch.substring(17,3);
String varr1 = textforthespeacch.substring(0, 16);
String varr2 = textforthespeacch.substring(21);
The problem is that the program crash on the first substring(17,3)
The text in the textforspeech is: "upload completed 100 0" The number 100 present percentages and will be all the time 100. And the number 0 present seconds it can be changed sometimes 0 sometimes 1 and sometimes 34.5
In varr I need to put the 100 as string "100" In varr1 "upload completed" in varr2 "0"
varr2 might be changed to can be "0" or "1" or "45.5" the others varr and varr1 never change the text "100" and "upload completed" never change.
This is the exception message in the logcat i'm getting:
09-15 18:45:45.435 5583-5583/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.webservertest, PID: 5583
java.lang.StringIndexOutOfBoundsException: length=22; regionStart=17; regionLength=-15
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.adilipman.webservertest.MainActivity$2$1.run(MainActivity.java:235)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
textforthespeacch.substring(17,3);. This should betextforthespeacch.substring(3, 17);if (connectedSuccess == true)is invitation to problems likeif (connectedSuccess = true)(=is assignment, not comparison) so you should avoid this style. It is better to useif (connectedSuccess)(orif (!connectedSuccess)instead of==false). You can also try yoda-styleif (true==connectedSuccess), it is safe since it is impossible to assign any value totrueliteral.string.substring(17, string-length()-2);(note it might be - 3, I'm not sure)