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
Related
I have this api link:
http://www.xxxx.com/?apikey=mykey&i=tt036543
Now by retrofit I am trying to call this api with GET method so I can create this interface:
#GET("/?apikey=mykey&i={movie_id}")
Call<MovieDetailEntity> getSearchedMovie(#Path("movie_id") String id);
But I got this error:
java.lang.IllegalArgumentException: URL query string "apikey=mykey&i={movie_id}" must not have replace block. For dynamic query parameters use #Query.
for method BatmanService.getSearchedMovie
If I use this Query :
#GET("/?apikey=mykey&i=")
Call<MovieDetailEntity> getSearchedMovie(#Query("movie_id") String id);
Then the link changes to :
http://www.xxxx.com/?apikey=mykey&i=&movie_id=tt036543
How can I call this api?
If I remember well once you use #query you should not set the variable at the url. It will use the string inside the #query annotation as the variable name.
This should work:
#GET("/?apikey=mykey")
Call<MovieDetailEntity> getSearchedMovie(#Query("i") String id);
And also this, if you want to pass your api key as well:
#GET("/")
Call<MovieDetailEntity> getSearchedMovie(#Query("apikey") String mykey,
#Query("i") String id);
The ? and & are automatically added for you.
Example URL : https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw
I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)
Subhendu Mondal decision crashes on links like this.
// args after id
https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw?view_as=subscriber
//minus sign in id
https://www.youtube.com/channel/UCQ18THOcZ8nIP-A3ZCqqZwA
so here is improved decision, also protocol group was removed from match result
^(?:http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,}\/channel\/([a-zA-Z0-9_\-]{3,24})
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var match = str.match(pattern);
console.log(match[1]);
UPDATE:
Here is the shortest way:
youtube.com\/channel\/([^#\&\?]*).*
Parse the string into a URI:
Uri uri = Uri.parse("https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw")
String id = uri.lastPathSegment() //UCAoMPWcQKA_9Af5YhWdrZgw
This is the pattern you can use to capture the channel id and this will validate the url also
^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$
I have no idea how to execute regex in android but sharing the regex url, you can check from here https://regex101.com/r/9sjMPp/1
Or a javascript code to perform
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var matchs = str.match(pattern);
console.log(matchs[2]);
// output is UCAoMPWcQKA_9Af5YhWdrZgw
Hope you get the idea.
You can use this regex for retrieving video ids from different types of youtube url.
String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
Then generate a Pattern instance using the above regex, from which you can get a Matcher instance by using Pattern.matcher(videoLink).
Now the Matcher.find() will give you a boolean on whether the above regex occurs in the given video link or not.
Depending on that you can use Matcher.group(), which will give you back the video id.
Refer Here
I am buidling a uriPatternMatcher for my content provider.
My uri is like content://com.example.wordapp/wordlist?flashid=1 which I got using Logger statement.
What should be my pattern matcher?
I tried
public static final String CONTENT_AUTHORITY ="com.example.wordapp";
public static final Uri BASE_CONTENT_URI =Uri.parse("content://"+CONTENT_AUTHORITY);
public static final String PATH_WORD ="wordlist";
public static final String FLASHCARD_ID="flashid";
matcher.addURI(authority, WordContractClass.PATH_WORD + "?"+ WordContractClass.WordListEntry.FLASHCARD_ID+"=#", WORD_WITH_FLASH_LIST);
The matcher is showing output as No Match Found.
You are adding the pattern incorrectly. From the documentation of UriMatcher(http://developer.android.com/reference/android/content/UriMatcher.html), you see that your second argument should be the path and third one should be the code against which your uri should be matched.
I am not sure what exactly you are trying to match but yours should look something like:
// assuming pathWord is the path to the list and WORDS_WITH_FLASH_LIST is the code that will be returned on a successful match
matcher.addURI(authority, pathWord + "/#", WORDS_WITH_FLASH_LIST);
The "#" implies that there are multiple entries within this path. You do not need the "?" that you currently have.
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?
I am writing a unit test in my Android app to test a ContentProvider. This test extends ProviderTestCase2. I have the following code:
// Tests the MIME type for the recent_searches table URI.
String mimeType = mMockResolver.getType(SearchEntryProvider.CONTENT_URI);
assertEquals(SearchEntryProvider.CONTENT_TYPE, mimeType);
The mock ContentResolver sees the value of SearchEntryProvider.CONTENT_URI as:
url = {android.net.Uri$StringUri#831696969096}"content://com.eazyigz.provider.RussiaMediaSearch/searches"
The problem is that this assertEquals fails because it expects a CONTENT_TYPE of
vnd.android.cursor.dir
but instead receives
vnd.android.cursor.item
For the life of me, I cannot figure out how to get the mimeType to be vnd.android.cursor.dir. Anybody have experience with this?
Thanks,
Igor
For anyone who is interested, the problem was in my ContentProvider's implementation of getType method. I had to implement it like this to return the correct CONTENT_TYPE:
#Override
public String getType(Uri uri) {
final int match = sUriMatcher.match(uri);
switch (match) {
// If the pattern is for searches, returns the general content type.
case SEARCHES:
return CONTENT_TYPE;
case SEARCH_ID:
return CONTENT_ITEM_TYPE;
default:
throw new UnsupportedOperationException(INVALID_URI + uri);
}
}