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 "
Related
This question already has an answer here:
How to find resource id of a file located in /res/raw folder by its filename?
(1 answer)
Closed 2 years ago.
I have an array of strings (for sound file names) , and i have the exact files attached to the projects as resources (raw), i am trying to fetch the ID of these resources via code in order to change which file (resource) to be playing from within the code.
String[] phrasesDesc = {"doyouspeakenglish.m4a",
"goodevening.m4a",
"hello.m4a",
"howareyou.m4a",
"ilivein.m4a",
"mynameis.m4a",
"please.m4a",
"welcome.m4a"
};
int index = 0;
for (String str : phrasesDesc) {
String res_name =phrasesDesc[index] ;
resourceID[index] = this.getResources().getIdentifier(res_name, "raw", this.getPackageName());
System.out.println(res_name +" "+ resourceID[index]);
index++;
}
please help.
enter image description here
You need to query them without the file-type extension:
String[] phrasesDesc = {"doyouspeakenglish", "goodevening", "hello", "howareyou", "ilivein", "mynameis", "please", "welcome"};
or somehow strip the file-type extension, eg: res_name.replace(".m4a", "").
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");
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");
I want to set pageToken to get items stored at Google Cloud Storage. I'm using Google API Client Library for Java v1.19.x.
I have no idea to generate pageToken from file path(or file name).
2 files stored in bucket.
my-bucket
/test.csv
/test2.csv
When I tried Google APIs Explorer with following parameters, I could get nextPageToken Cgh0ZXN0LmNzdg==.
And I found out that I can get test.csv string by decoding nextPageToken with base64.
bucket: my-bucket
pageToken:
prefix: test
maxResults: 1
{"kind": "storage#objects", "nextPageToken": "Cgh0ZXN0LmNzdg==", ...}
But How can I get Cgh0ZXN0LmNzdg== from test.csv?
Although I tried Base64 encoding, result didn't match.
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
String lastFile = "test.csv"
String token = Base64.encodeBase64String(lastFile.getBytes());
String bucket = "my-bucket"
String prefix = "test"
Storage.Objects.List listObjects = client.objects().list(bucket);
listObjects.setPrefix(prefix);
listObjects.setPageToken(token);
long maxResults = 1;
listObjects.setMaxResults(maxResults);
do {
Objects objects = listObjects.execute();
List<StorageObject> items = objects.getItems();
token = objects.getNextPageToken();
listObjects.setPageToken(token);
} while (token != null);
I could get next token from file path string using following codes by myself.
How to get nextToken from path string
String nextToken = base64encode(0x0a + asciiCode + pathString)
asciiCode can be taken between 0x01(SOH) and 0x7f(DEL). It seems to depend on path length.
my-bucket/
a/a(3byte) 0x03
a/ab(4byte) 0x04
test.txt(8byte) 0x08
Notice
If path length is longer than 1024 byte, another rule seems to apply. But I couldn't found out rules.
See also Object Name Requirements
import com.google.common.io.BaseEncoding;
String lastFile = "test.csv"
String token = base64Encode(lastFile);
String bucket = "my-bucket"
String prefix = "test"
Storage.Objects.List listObjects = client.objects().list(bucket);
listObjects.setPrefix(prefix);
listObjects.setPageToken(token);
long maxResults = 1;
listObjects.setMaxResults(maxResults);
do {
Objects objects = listObjects.execute();
List<StorageObject> items = objects.getItems();
token = objects.getNextPageToken();
listObjects.setPageToken(token);
} while (token != null);
private String base64Encode(String path) {
byte[] encoding;
byte[] utf8 = path.getBytes(Charsets.UTF_8);
encoding = new byte[utf8.length + 2];
encoding[0] = 0x0a;
encoding[1] = new Byte(String.valueOf(path.length()));
String s = BaseEncoding.base64().encode(encoding);
return s;
}
I know this question is already answered and is applied to Java, I'd like to mention that this question applies to PHP as well.
With the help of the approved post from sakama above I figured out a PHP version of his solution.
The PHP equivalent for generating the token is as follow:
base64_encode(pack('c', 0x0a) . pack('c', $path_string_length) . pack('a*', $path_string));
The byte pattern seems indeed (as sakama already mentioned) to be:
<line feed><line data length><line data>
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