If I have a url: http://www.test.com/segment1/segment2/name:tom/segment4/
What is the best way to set a variable equal to "tom"? I figure there's a method that I'm unaware of. For now I'm just parsing but if a method is optimized to do this I'd rather use that.
Thanks
If the url will always be formatted the same parsing is probably fine.
//Parse name from url
public String getName(String url){
String[] spliturl = url.split(":");
String[] arrayofnames = spliturl[1].split("/"); //Segment after the semicolon
return arrayofnames[0]; //But before the /
}
How are you doing it currently?
Related
I am developing a Weather app ,in that trying to build Uri that looks like
Content://com.example.weather.app/Location/locationName?Date=12012017
The documentation says Uri reference has pattern as ://?#
trying to understand this following code
public static Uri buildWeatherLocation(String locationSetting) {
return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
}
public static Uri buildWeatherLocationWithStartDate(
String locationSetting, long startDate) {
long normalizedDate = normalizeDate(startDate);
return CONTENT_URI.buildUpon().appendPath(locationSetting)
.appendQueryParameter(COLUMN_DATE,Long.toString(normalizedDate)).build();
}
what is the actual difference and when we use appendPath() and appendQueryParameter() methods?
why can't we use appendQueryParameter() for locationSetting ,bit confusing suggestions plz
appendPath() is for path segments and appendQueryParameter() for query params with key value (in your example Date=12012017).
Check this link for more info and examples:
Use URI builder in Android or create URL with variables
appendQueryParameter is for query string parameters and appendPath is for site path
I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);
I'm using a web view in my app and load the url "http://google.com"
now I want to automatically search the word that I input in a TextView.
thnx for answering I just use this and it works
String qString ="http://www.google.com/search?q=%s";
String test="dogs";
String q = qString + test;
web.loadUrl(q);
a very simple way. sigh
Try this:
http://www.google.com/search?q=dogs
Replace the dogs with whatever was in the text field.
Use String.format(..) method to place the arguments in the string. So
String qString ="http://www.google.com/search?q=%s";
is the query string.
String test="dogs"; // or whatever.
String.format(qString, test);
I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL
I have a url which have a response in Json
lon=-0.1275&pg=0
I parsed the data from it and displayed in ListView, after displaying the 20 fields the above url should be changed to
lon=-0.1275&pg=1
ie the "pg" must change from 0 to as many pages. How to do that?
please provide me some help
Thanks in advance
You can have a class level variable
int page = 0;
and when you make your url just append it like this:
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg="+page;
and every time increment it with your pagination like this:
page+=10;
do the request again.
You may use string's replace method to modify url string:
String url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=0";
url = url.replace("&pg=0", "&pg=1");
I think this way, NOT TESTED.
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=";
int i=0;
do{
tempURL = url+i;
// get tempURL response
if(response==null) break;
else
{
// parse response
i++;
}
}while(response!=null)