This question already has answers here:
How to parse or split URL Address in Java?
(4 answers)
Closed 3 years ago.
I want to get "BusinessId" from this URL in Java: https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189
What can i do?
I need some help, please :C
You could try using String#replaceAll for a one-line solution:
String url = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
String businessId = url.replaceAll(".*[&?]businessId=([^=?&]+)\\b.*", "$1");
System.out.println(businessId);
This prints:
5d8648b561abf51ff7a6c189
Actually Apache has a number of libraries that can make handling your requirement much easier. On Android, the following might work:
Uri uri = Uri.parse(url);
String linkParam = uri.getQueryParameter("link");
Uri uri2 = Uri.parse(linkParam);
String businessId = uri2.getQueryParameter("businessId");
I noticed android tag in your question. You can parse the Url string to Uri and then you can get query param.
String myUrl = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
Uri uri = Uri.parse(myUrl);
String businessId = uri.getQueryParameter("businessId");
Related
I have the following URl http://127.0.0.1/?code=AQABAAIAAAAGV_bv21
I need to capture the chracters after code=
but every time the URL is loaded that code is different..
I had something like this but since its dynamic I can not do this..
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
String code = url.substring(url.length() -10);
you can use something like below :-
String code = url.split("?code=")[1];
if you are on Android:
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
Uri uri = Uri.parse(url);
String code = uri.getQueryParameter("code");
or try the following regexp:
(\?|\&)([^=]+)\=([^&]+)
Try this.
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21";
String codeValue = url.replaceAll(".*code=([^&]*).*", "$1");
System.out.println(codeValue);
output:
AQABAAIAAAAGV_bv21
This method works even if other parameters are added. For example http://127.0.0.1/?id=123&code=AQABAAIAAAAGV_bv21&opt=yes
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
Okay so I'm trying to parse the url and extract partial data.. However it doesn't seem to be extracting the exact data I want.. I assume it's extracting ID and not Value.
This is the code I'm using
price = readUrl(apiUrl + String.valueOf(id)).split(",")[1].split(":")[1];
String price2 = price.substring(0, price.length() - 1);
return Integer.parseInt(price2);
the url I'm using is
https://api.rsbuddy.com/grandExchange?a=guidePrice&i=
parameter i = id of item, for this example we will use " 2619 "
which returns,
{"overall":49907,"buying":0,"buyingQuantity":0,"selling":49907,"sellingQuantity":2}
the information I want is
49907
from
{"overall":49907,
Use JSONObject:
JSONObject jsonObject = new JSONObject(YOUR_STRING_YOU_WANT_TO_PARSE);
Integer price = jsonObject.getInt("overall");
What you get from API is a JSON. So you can simply use JSONObject.
https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
You can do something like:
jsonObject.getInt("overall");
This question already has answers here:
How to parse or split URL Address in Java?
(4 answers)
Closed 6 years ago.
I have a URL like this:
http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394
How to get the value of parameter of chapter and post?
My URL contains '&' in the value of chapter parameter.
You can use the Uri class in Android to do this; https://developer.android.com/reference/android/net/Uri.html
Uri uri = Uri.parse("http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394");
String server = uri.getAuthority();
String path = uri.getPath();
String protocol = uri.getScheme();
Set<String> args = uri.getQueryParameterNames();
Then you can even get a specific element from the query parameters as such;
String chapter = uri.getQueryParameter("chapter"); //will return "V-Maths-Addition "
I m new in GWT and I m generating a web application in which i have to create a public URL.
In this public URL i have to pass hashtag(#) and some parameters.
I am finding difficulty in achieving this task.
Extracting the hashtag from the URL.
Extracting the userid from the URL.
My public URL example is :: http://www.xyz.com/#profile?userid=10003
To access the URL in GWT you can use the History.getToken() method. It will give you the entire string that follows the hashtag ("#").
In your case (http://www.xyz.com/#profile?userid=10003) it will return a string "profile?userid=10003". After you have this you can parse it however you want. You can check if it contains("?") and u can split it by "?" or you can get a substring. How you get the information from that is really up to you.
I guess you already have the URL. I'm not that good at Regex, but this should work:
String yourURL = "http://www.xyz.com/#profile?userid=10003";
String[] array = yourURL.split("[\\p{Lower}\\p{Upper}\\p{Punct}}]");
int userID = 0;
for (String string : array) {
if (!string.isEmpty()) {
userID = Integer.valueOf(string);
}
}
System.out.println(userID);
To get the parameters:
String userId = Window.Location.getParameter("userid");
To get the anchor / hash tag:
I don't think there is something, you can parse the URL: look at the methods provided by Window.Location.
This question already has answers here:
Parsing result of URL.getHost()
(2 answers)
Closed 8 years ago.
I need to parse url in my java code and get the domain. I wrote the following code:
static String domain(URL url) {
String host = url.getHost();
int i = host.lastIndexOf('.');
if(i == -1){
return "Not domain";
}
if (i ==0 ){
return "Not domain";
}
String domain;
i = host.lastIndexOf('.', i - 1);
if (i == -1) {
domain = host;
}
else {
domain = host.substring(i + 1, host.length());
}
}
This code parses domains like example.com
But how can my code parse domains like exmaple.co.ir , subdomains.example.co.ir and the others extensions like co.uk, org.ir and so on.
EDIT
my url is http//blog.example.co.ir/index.php or http//blog.example.co.uk/something.html
my goal is to print:
example.co.ir and example.co.uk
The problem is that your parsing code is limited to domains with just one dot. You can use regular expressions or recursive parsing to solve this problem. This is one way of approaching this problem.
I believe this work for any kind of URL(in correct URL format)
domain= host.split("/")[2];
Note:
split("/") will create an array from the String, for example:
String host="http//blog.example.co.ir/index.php";
host.split("/") will give you array of String: [http, ,blog.example.co.ir, index.php]
And your desired output is at index 2