Only get part of string in android? - android

Okay I wasn't really sure how to word this question, but basically what I want to do is, I got a url from a webView in android, and I need to put part of that url into a string, the url will look something like this: http://localhost/?code=4/3pakksajdfASDFwek.4nsKfAYN7XQVshQV0ieZDAp-PrgEcAI and I only want the part after code=, is that possible? Thanks

int start = my_string.indexOf("=");
String suffix = my_string.substring(start + 1);

If other parameters can be on the URL, or code is not always first parameter:
String url = "http://localhost/?code=4/3pakksajdfASDFwek.4nsKfAYN7XQVshQV0ieZDAp-PrgEcAI";
String code = url.replaceAll(".*(?:[?]|[&])code=([^&]+)","\1");
tests here: http://fiddle.re/nxfv

If you are totally sure the URL never has other form, you can use indexOf and substring.
Otherwise, it is better if you use URI class to extract out the query part of the URL (use getRawQuery just to be safe), then tokenize it with split along & character and find the correct key-value pair to obtain the correct value. This method is not as brittle as indexOf method above.

Related

GET URL with constant and dynamic params, with Retrofit2

I have a GET URL like that:
http://myrestapi.com/?method=search&name=nametosearch&format=json
I've written my service like that:
#GET("?method=search")
Observable<List<Album>> getAlbums(#Query("name") String searchedName);
Unfortunately, I don't know how to add &format=json at the end.
I've tried:
#GET("?method=search&name={searched_name}&format=json")
Observable<List<Album>> getAlbums(#Path("searched_name") String searchedName);
But it doesn't work since searched_name is not a Path element.
Can you please help me with that?
If you append &format=json after ?method=search and use #Query("name") then name will be appended after the format parameter. If the server is handling the parameters correctly the order shouldn't matter.
i.e.
#GET("?method=search&format=json")
Single<List<Album>> getAlbums(#Query("name") String name);
Would translate to: http://myrestapi.com/?method=search&format=json&name=name

Why do Glide only work on raw string url data? (Android Studio)

I can successfully use a string url with glide to get the image I want:
glideVariable!!.loadImageUrl("https://firebasestorage.googleapis.com/v0/b/healthandchocolate-46f3d.appspot.com/o/cookies%2FcookiesImg1.jpg?alt=media&token=63721d7d-207e-441c-9387-14dced13d3c8")
but when I try to use the very same url, stored in a variable, the image doesn't load:
glideVariable!!.loadImageUrl(recipeArray[1].recipeImage.toString())
I have done a Log.d on recipeArray[1].recipeImage.toString() and it does indeed contain the very same url that worked on my first example, using raw string data.
I have used escape commands to encapsulate the variable in quotation marks like this:
glideVariable!!.loadImageUrl("${recipeArray[1].recipeImage.toString()}")
but it still doesn't work. I have also tried to cast it as URL and URI, but still nothing. Any ideas? Can glide only use raw string data?
UPDATE
I noted that recipeArray[1].recipeImage contain both a key and a value. Perhaps this is the problem. It doesn't access the url string directly. How do I make sure just to use the string value? It looks like this:
DataSnapshot { key = recipeImageFirebase, value = https://firebasestorage.googleapis.com/v0/b/healthandchocolate-46f3d.appspot.com/o/cookies%2FcookiesImg1.jpg?alt=media&token=63721d7d-207e-441c-9387-14dced13d3c8 }
OK, got it now. The recipeArray[1].recipeImage.toString() was a data snapshot containing both a key and a string. I went back to when I stored the variable:
val image = item.child("recipeImageFirebase")
tempRecipe.recipeImage = image.toString()
and changed the last part to
tempRecipe.recipeImage = image.value.toString()
Now it only contains the string value and not the key "recipeImageFirebase" as well!

How to get Custom Url

In my java file URL is static like
String URL = "https://aa.com/hospital/dmc.jpg";
But I want the URL will come from database like
https://aa.com/hospital/dmc.jpg
and after adding " sign at the first and the last word and put into a string.
How can I get that?
You can do it by replace() function
url = url.replace("https:","\"https:").replace(".jpg",".jpg\"");
You can get more information here
question

how to find if the input text is a url using regex

I want to check if the input received is url using REGEX. How can i do it ? Is there any standard REGEX available that works for checking url?
my requirement is to check if the input text is a url or not, but not if the text contains a url.
you can check without regex in android
android.util.Patterns.WEB_URL.matcher(linkUrl).matches();
I would strongly recommend not using a Regex in this situation as there are a lot of different cases that can constitute a valid URL (see the top voted answer to this question for an example of an RFC valid regex). If working on Android I would recommend using the Uri class;
String urlToValidate = "http://google.co.uk/search";
Uri uri = Uri.parse(urlToValidate);
You can then look at the different parts of the URL to ensure that you are willing to accept the URL input. For example;
uri.getScheme(); // Returns "http", if you only want an HTTP url
uri.getHost(); // Returns "google.co.uk", if you only want a specific domain
uri.getPath(); // Returns "/search"

Linkify Android Transform Question

Trying to do something fairly simple.
Taking text like this
User Name: This is a comment I am making
It is in a single TextView. I want to make the User Name a link. I decided that the easiest thing would be to surround the User Name with something like "$#" so it becomes
"$#User Name:$# This is a comment I am making
That way I can use the following regular expression
Pattern userName = Pattern.compile(".*\\$#(.+)\\$#.*");
with Linkify and make it a link. However, clearly I need to remove the delimiters, so the following is the code
title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {
#Override
public String transformUrl(Matcher match, String url) {
return match.group(1);
}
};
Linkify.addLinks(title, userName, "content://user=", null, transformer);
For some reason however, the whole text becomes one giant link, and the text isn't being transformed at all.
It actually did turned out to be pretty easy. I ended up not using the crazy "$#" to delimit the username, instead sticking with just
User Name: This is a comment I am making
so I ended up using the following pattern
Pattern userName = Pattern.compile("(.+:)");
Very simple, and the code becomes just
title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");
Thank you to nil for the original suggestion. I was indeed matching the whole string instead of just the userName which is the link.
My best guess is the regex you're using is the problem, where you're telling it to basically pick out the entire string if it matches, including everything before and after what you're looking for. So, the TransformFilter is probably being passed the entire matched string. transformUrl as far as I can tell expects you to return the URL, so the entire string is linked to the first match group.
So, with that in mind, it's probably in your best interest to change the regex to something along the lines of "\\$#(.+?)\\$#" (with an added ? in the group to make the match non-greedy) so as to avoid matching the entire string and just picking out the bit you want to URL-ize (for lack of a better term, plus adding -ize to words sounds cool).
Why not put the delimiters inside the pattern to change ?
Pattern userName = Pattern.compile(".*(\\$#.+\\$#).*");
Then change the transform filter to remove the start and end patterns when changing into the URL...

Categories

Resources