Reputation: 403
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)
Upvotes: 0
Views: 75
Reputation: 5658
With this line
String varr = textforthespeacch.substring(17,2);
The first value in a substring
call must be less than the second. That is why you get regionLength=-15
.
Upvotes: 1
Reputation: 2823
Here is the syntax for the substring()
method:
public String substring(int beginIndex)
or
public String substring(int beginIndex, int endIndex)
where
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclusive.
How can the endIndex
in: String varr = textforthespeacch.substring(17,3);
be less than the beginIndex
.
I think it should be the other way round.
Upvotes: 1