I'm developing an app for M2M communication via SMS between the mobile phone and an electronic device which has a SIM card.
When I send a command from the mobile to the device, this device returns a confirmation string to the mobile which has this structure.
Confirmation OK:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. OK
Confirmation ERROR:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. ERROR
Now in my app I have to manage this message to get the relevant information. For example, the first part of the message is the identify code (DEV33456) so to get it I split the message:
String[] separated = message.split(",");
String ident = separated[0]; //DEV33456
But now, I need to get the last word (OK or ERROR) to determine if the message is OK or not. To do that I thought that the easiest way should be to split the message using the point before the OK or ERROR word. Is the only point in the entire message, so it should split it like this:
String[] separated = message.split(".");
String unused = separated[0]; //DEV33456, 15, Date: yy/mm/dd-hh:mm:ss
String error = separated[1]; //OK or ERROR
But it is throwing an ArrayIndexOutOfBoundsException and debuging it I see that after the first line where the separated string array should have a lenght of 2, it has a lenght of 0.
Is there any problem on spliting the string using a "."? I have done the same procedure to split it using a "," and it has done int correctly.
Java split uses regular expressions, . is a special character in regular expressions.
You need to escape it by using the string "\\.".
why not just use
message.endsWith("OK")
Check if this is want you want:
if(message.substring(message.length()-2, message.length())
.toLowerCase().contains("ok"))
Log.d("yes");
else
Log.d("no");
Hopefully it is.
Related
I've tried to write a java client server application for chatting between clients privately. I am searching for a way to write the command to send a message to one or more receivers, I thought of handling it with a command like:
/send UserName UserName msg
but I pretty soon discovered that the character in between usernames and the message couldn't be repeated in the message because then it wouldn't know what is the message and what are the recipients, using a prearranged character also seemed to have some implications, like the inability to use that character in usernames or the message.
What would be a good way to handle such a thing?
You can determinate what is message and what are usersNames by splitting on last and first occurrence of delimiter in your case empty string. Lets say user input is inputStr
int firstOccurance = inputStr.indexOf(" ");
int lastOccueance = inputStr.lastIndexOf(" ");
And split it like this
String command = inputStr.substring(0,firstOccurance); // /send
String users = inputStr.substring(firstOccurance+1,lastOccurance); //UserName UserName
String msg = inputStr.substring(lastOccurance+1); //msg
Using space as delimiter will lead to a issue that message itself cannot include a space.
Instead you can use special delimiter to indicate a particular substring is a message.
Some very common delimiters are | ;
I'm making a program that gets data from league of legends api, it analizes your current game and shows you your oponents names, ranks and stuff. But i have an issue because to get someone's rank i need to travel to a specific url with his nick in it. The problem is that some players put characters like å, à, è, ö, or stuff like that in their nicks, and when that happens my program stops working. I will show you part of the code that im using to do that:
getSummonerID2 = new JSONObject(IOUtils.toString(newURL("https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/"+ newPlayerNick[i] + "?api_key=" + APIKEY), Charset.forName("UTF-8")));
This is the error it throws:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/JegFårDetDårligt?api_key=( My key, cant show it sry)
It happened to me before with the spaces, because it looks like you cant even put spaces in a url, so the thing i did to solve that was:
newPlayerNick[i] = playerNick[i].replaceAll("\\s+","_");
That basically replaces every space with a _, so it works perfectly, but i cant replace an "å" with a normal "a" as they are different characters.
So, do you know any solution for this? thank you so much guys.
You can try URL encoding for special characters:
URLEncoder.encode("This string has å à è ö and spaces","UTF-8")
the output is like:
This+string+has+%C3%A5+%C3%A0+%C3%A8+%C3%B6+and+spaces
You can try this url to see if it works:
https://www.google.com.hk/search?q=This+string+has+%C3%A5+%C3%A0+%C3%A8+%C3%B6+and+spaces
you can see it is an google link and the keyword is passed
I want to explain my problem about a command string submitted from the client to the server. Through this specific command I have to login into the database managed by the server. According to the project's guide lines, command's formatting is: login#username;password. The string is sent to the server through a socket. User inserts his credentials and these are sent to the server putting them into the command. The server has to split the user and the password from the command and has to check, in the database, if the user already exists. Here is my problem, given the fact that in the user and in the password can be present ; (the same character of the separator), I don't understand (server side) how can i divide user and psw from the command so that user and psw are the same of the ones inserted in the client.
Have you got some ideas about? I was thinking to use regular expression, is this the right way?
I would just split the string into a user/pass string like this:
String userPass = "User;Pass";
String[] parts = userPass.split(";");
String user = parts[0];
String pass = parts[1];
If you really have to split a string by a separator included in both substrings there is no way to make sure the string is always split correctly!
I think you can use the unicode unit separator character 0x001F to separate you strings safely, as the user will have some difficulties entering such a character!
But depending on your application and string processing this could cause damage, too, as there are some issues concerning incompatibilities (e.g. xml 1.0 doesn't support it at all).
Otherwise if only one or none of the substrings may contain such a character you can easily use one of the already presented methods to split up the string and safely extract the data.
This will work only if User/password doesn't contain # or ;
String loginStr = "login#User;Password";
String []splittedLogin = loginStr.split("#");
String []loginCredentials = splittedLogin[1].split(";");
String user = loginCredentials[0];
String password = loginCredentials[1];
System.out.println(user);
I am getting cookie value in jstring. Server is sending it as base64encoded UTF8 string. I compared string from server and my end, and I am getting exactly same string.
Now I need to decorate this value with n= as prefix and ; as suffix. (Which I am doing in line no. 2 of code).
If I do not use line no. 1, string goes null to Java Server. Otherwise server is getting value.
jstring = [jstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *cookieVal=[NSString stringWithFormat:#"n=%#%#",jstring,#";"];
[self.requestSerializer setValue:cookieVal forHTTPHeaderField:#"Cookie"];
We are using AFNetworking in iOS for request and response. We have observed very strange pattern,
If string contains /(forward slash) then we are getting padding error on Java server, if string doesn't contain /, then string will go as required.
As you can see in line no. 3, we are sending this value as header of http/https request.
I have tried many things, like this (tried very last code with my string.). Also, tried to use different encoding, but problem still persists.
This url conversion would not convert all the special characters we have in ios device keypad.
we have to convert this with blow function. use this as category.
- (NSString *) URLEncodedString_ch {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]%~_. ", CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
}
I'm currently in the progress of making a java application, one of the functions is showing related emails and documents.
But the full path to the email (on a sharepoint server) is displayed in the application, for obvious reasons the number of characters depends on the title of the email and the location.
But they all have the same in common, there are ALWAYS 3 slashes in the title.
Like this: Myserver/client/caseID/Title of Email here
Is it possible to get Java to "count" the number of slashes and just delete everything before the third slash?
Use the Split function to achieve this.
String value="Myserver/client/caseID/Title of Email here";
value=value.split("\\/")[3];
System.out.println("your value is "+value);
Here is an example:
String s = "Myserver/client/caseID/Title of Email here";
int i = s.lastIndexOf('/');
if (i != -1)
System.out.println(s.substring(i));
else
System.out.println("no slashes");
Using the replaceFirst function is one way to go:
String yourString = "Myserver/client/caseID/Title of Email here";
System.out.println(yourString.replaceFirst("([^/]+/){3}", ""));
try this
s = s.replaceFirst(".*?/.*?/.*?(?=/)", "");