I construct a URL with the following bit of code:
String login = rootActivity.getString(R.string.url_authentication);
login = login + "user=" + mySharedPreferences.getString("username", "invalid") + "&" + "key=" + mySharedPreferences.getString("key", "invalid");
login = login.toLowerCase(Locale.US);
System.out.println("Logging in at " + login);
new HttpConnection(handler).get(login);
The URL is valid, as far as I can see visually, but the HttpConnection fails because there's an illegal character in the URL at the index of the ampersand. What really flummoxes me is, the app has between 1,000 and 5,000 installs, and we have a total of two reports of this over the past year – both from American users using Samsung devices, so I doubt it's a character encoding issue.
Don't forget to urlEncode your parameters.
login = login + "user=" + URLEncoder.encode( mySharedPreferences.getString("username", "invalid") ) + "&" + "key=" + URLEncoder.encode( mySharedPreferences.getString("key", "invalid") );
Don't know what login is, but is there a question mark on the end of it? If you're don't something like www.example.comuser="something"&key="somethingElse" then that won't work.
Having a raw & (amplisand) will lead to an error, you should encode your special characters.
Do this:
String login = URLEncoder.encode(login);
http://developer.android.com/reference/java/net/URLEncoder.html#encode(java.lang.String)
Related
I want use register in my application and i should send password and verifyCode with SMS to users phones.
But i should read verifyCode from message and set automatically number into verifyCode EditText.
My message format :
Hi, welcome to our service.
your password 12345
your verifyCode 54321
How can i do it? Please help me <3
Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.
String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);
Here String verifyCode = numberOnly.substring(6); is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10); to avoid confusions.
But this is prone to errors like StringIndexOutOfBoundsException, So whenever you want to get substring which is starting from index i till the end of the string, always write numberOnly.substring(i).
There are a lot ways to do this. You can use some complicated regex or use a simple spilt method.
Try this,
String str = "Hi, welcome to our service.\n"
+ "\n"
+ "your password \n"
+ "12345\n"
+ "\n"
+ "your verifyCode \n"
+ "54321";
// Solution #1
String[] parts = str.split("\n");
System.out.println(parts[3]);
System.out.println(parts[6]);
// Solution #2
String PAT = "(password|verifyCode)\\s+(\\d+)";
Pattern pats = Pattern.compile(PAT);
Matcher m = pats.matcher(str);
while (m.find()) {
String grp = m.group(2);
System.out.println(grp);
}
I am working on an application that sends an e-mail with several information. Among the information is a link to the location of the user to Google Maps. A sample of the link would be:
http://www.google.com.ph/maps/place/14°39'3.8952"N121°2'57.4116"E/#14.651082,121.049281,17z
The code looks like this:
"Google Maps Link: http://www.google.com.ph/maps/place/" +
degToDMS(location.getLatitude()) + "\"N" +
degToDMS(location.getLongitude()) + "\"E/#" +
location.getLatitude() + "," + location.getLongitude() + ",17z");
I converted the degrees longitude and latitude to DMS using a formula I found online and it seemed to be returning good data. However, when I go check the email, the link looks like:
http://www.google.com.ph/maps/place/14°39'3.8952"N121°2'57.4116"E/#14.651082,121.049281,17z
and the hyperlink ends at the first double quote (") right before the N. This is actually a bit irritating and troubling because it doesn't link the entire link properly and it is cut.
How can I escape a double quote in a link? Or is there a better way to link to Google Maps?
I used URLEncode, thanks to for3st for the lead, as such:
final String urlRaw = degToDMS(location.getLatitude()) + "\"N" +
degToDMS(location.getLongitude()) + "\"E/#" +
location.getLatitude() + "," + location.getLongitude() + ",17z";
String encodedURL = "";
try {
encodedURL = URLEncoder.encode(urlRaw, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
where urlRaw is the "problematic' part of your url (where characters don't get escaped properly or something). You DON'T put in the http://.... because it will appear as such http%3A%2F%2Fwww.google.com.ph and yeah you don't want that.
Hence, I only escaped the latter part of my url, the one that starts with the DMS coordinates.
Once I have my encodedURL variable, I simply concat it to the rest of the starting URL as such:
String link = "Google Maps Link: http://www.google.com.ph/maps/place/" + encodedURL;
and not it works.
I'm developing a game with Unity3D for Android. I'm using Facebook App for sharing game score in Facebook. But I receive a error message ;
My codes are here ;
//facebook share start
public static void share(string link, string pictureLink, string name,string caption, string description, string redirectUri){
Application.OpenURL(ShareUrl +
"?app_id=" + AppId +
"&link=" + WWW.EscapeURL( link )+
"&picture=" + WWW.EscapeURL(pictureLink) +
"&name=" + WWW.EscapeURL(name) +
"&caption=" + WWW.EscapeURL(caption) +
"&description=" + WWW.EscapeURL(description) +
"&redirect_uri=" + WWW.EscapeURL(redirectUri));
}//facebook share end
if(GUI.Button(new Rect(Screen.width/2,(Screen.height/2-30),80,20), "Share")){
print("Share");
share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}
I tried to write many adresses (many many configurations) intead of "http://facebook.com", but I can't find the true one.
If you know the solution, can you halp me please?
I would like to thank you for your interest.
there must me an invalid parameter that you have provided . try to find it out and make the change it will solve your issue
try the share with first s in caps example
Share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}
I am using Rhomobile (for Android) to post status update on Twitter. For that, following the steps of Oauth implementation I am able to get logged in with Twitter but after login, when trying to post the status update everytime I got HTTP Response as {"errors":[{"message":"Could not authenticate you","code":32}]}.
Below is the relevant code for making the request to post status update.
def post_to_twitter(comment)
$rnd = rand(36**32).to_s(36)
$post_status_url = "https://api.twitter.com/1.1/statuses/update.json"
$oauth_token1 = #account_set.tt_oauth_token
$oauth_token_secret1 = #account_set.tt_oauth_token_secret
#oauth_nonce = $rnd
#oauth_timestamp = Time.now.to_i.to_s
#http_port = System.get_property('rhodes_port')
#url_param = "oauth_consumer_key="+ $oauth_consumer_key + "&" +
"oauth_nonce=" + #oauth_nonce + "&" +
"oauth_signature_method=" + $oauth_signature_method + "&" +
"oauth_timestamp=" + #oauth_timestamp + "&" +
"oauth_token=" + $oauth_token1 + "&" +
"oauth_version="+ $oauth_version + "&" +
"status=" + Rho::RhoSupport.url_encode("Test")
$oauth_sign = get_auth_signature($post_status_url, #url_param, "")
#auth_header = "OAuth oauth_consumer_key="+ $oauth_consumer_key + ", " +
"oauth_nonce=" + #oauth_nonce + ", " +
"oauth_signature=" + $oauth_sign + ", " +
"oauth_signature_method=" + $oauth_signature_method + ", " +
"oauth_timestamp=" + #oauth_timestamp + ", " +
"oauth_token=" + $oauth_token1 + ", " +
"oauth_version="+ $oauth_version + ", " +
"status=" + Rho::RhoSupport.url_encode("Test")
postTTres = Rho::AsyncHttp.post(
:url => $post_status_url,
:headers =>{"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => #auth_header }
)
p postTTres
end
The signature generation function is as follows:
def get_auth_signature (url, url_param, secret)
signature = "POST&" + Rho::RhoSupport.url_encode(url).to_s +
"&" + Rho::RhoSupport.url_encode(url_param).to_s
key = $oauth_consumer_secret + "&" + secret
hmac = HMAC::SHA1.new(key)
hmac.update(signature)
$signature = Base64.encode64("#{hmac.digest}")
$signature = Rho::RhoSupport.url_encode("#{$signature.gsub(/\n/,'')}")
return $signature
end
The parameters values when traced are as follows:
#url_param before generating signature ---------
oauth_consumer_key=2ChmEzWBe5Y9hMYODqA1IQ&oauth_non
ce=iyb9502vspjnhj9orb87sriych16678b&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1380117614&oauth_token=244586214-M6A2jMlR7vZiqwAMrfuSj7I7XFzFTRd4
6nV6aTLK&oauth_version=1.0&status=Test`
Passing #url_param to get_auth_signature() to generate signature.
Generated signature url and signature is
Signature string ----------
POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.
json&oauth_consumer_key%3D2ChmEzWBe5Y9hMYODqA1IQ%26oauth_nonce%3Diyb9502vspjnhj9orb87sriych16678b%26oauth_signature_method%3DHMAC-SHA1%26oauth_timesta
mp%3D1380117614%26oauth_token%3D244586214-M6A2jMlR7vZiqwAMrfuSj7I7XFzFTRd46nV6aTLK%26oauth_version%3D1.0%26status%3DTest"`
Base64 string -------
gjdXuU3qoGNt90Q2dRhNM3IXaBI%3D
Passing all these values as header Authorization to https://api.twitter.com/1.1/statuses/update.json
and got
Http Response as {"errors":[{"message":"Could not authenticate you","code":32}]}.
Also tried passing it as post parameters in :body but no luck.
postTTres = Rho::AsyncHttp.post(
:url => $post_status_url,
:headers =>{"Content-Type" => "application/x-www-form-urlencoded"},
:body => #url_param + "&oauth_signature=" + $oauth_sign
)
Checked the system timings with Twitter server timings and thats fine.
Also tried with static oauth_token that we can get from Twitter account but then too same response.
Please help me to fix this. I am unable to trace what I am missing or where I am going wrong.
Thanks
Two things:
The "status" parameter is a normal post data parameter, should not be in the authorization header, see https://dev.twitter.com/docs/api/1.1/post/statuses/update
There is a strange issue with API 1.1, with encoding special chars (like %20 for " "). The following did it for me: url encode the content of "status" for the input of the signature and do not encode it in the post data itself.
EDIT:
You forgot to supply the secret that corresponds to the user_token:
$oauth_sign = get_auth_signature($post_status_url, #url_param, "")
When you gain an access token from Twitter via https://api.twitter.com/oauth/access_token , twitter replies with an auth_token and an auth_token_secret (and the id and screen_name). You correctly supplied the auth_token in the params (and thereby to the signature), but you should supply the auth_token_secret where you currently put "".
EDIT 2:
Above edit makes me wonder: did you gain an access token using https://api.twitter.com/oauth/access_token ? Because for that you should use the oauth_token_secret that is a parameter in a call from twitter to the callback url supplied to https://api.twitter.com/oauth/request_token . This callback url is called as a response to confirmation of a user via https://api.twitter.com/oauth/authorize. For https://api.twitter.com/oauth/request_token you could use "" as a secret, since there is no auth_token yet in that phase (and thereby no corresponding secret).
I'm asking about the right way to concatenate a url with values !
here my example "http://10.0.2.2/myFolder/page.php?value1=!!&value2=!!,+value1,+value2"
I hope you will understand what I'm saying cause I'm new to Android and thank you.
The other answer is almost correct, but you should be URL encoding the keys and values:
String uri = "http://" + address + "/myFolder/page.php?" + URLEncoder.encode(value1) + "=" + URLEncoder.encode(value2);
you can use stringbuilder and append the as many lines and values you want to add
like wise in your case
StringBuilder sb = ("http://").append(address).append("/myfolder/page.php?").append(value1).append("=").append(value2);
and then use the following
sb.toString();
hope this might helps you.I have used this in my one of the projects
Jamal, Are you trying to concatenate the String: "http://10.0.2.2/myFolder/page.php?value1=!!&value2=!!,+value1,+value2" ?
If so, Please try the follwing:
String uri = "http://" + address + "/myFolder/page.php?" + value1 + "=" + value2;
Hope this helps