So I want to get the metadata of a youtube video (say this one: https://www.youtube.com/watch?v=qlTA3rnpgzU).
I'm going to encode it and wrap it in another url like so: http://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DqlTA3rnpgzU&format=json
My interface definition will look like this:
public interface YoutubeApi {
#GET ("oembed")
YoutubeMetaData metaData (#Query (QUERY_VIDEO_URL) final String url,
#Query(QUERY_FORMAT) final String alwaysJson);
}
That's all fine and dandy, but I don't ever want to specify any format other than JSON here (format=json is a fixed part of this url).
Is there a way to specify this in my interface declaration and reduce my interface to:
public interface YoutubeApi {
#GET ("oembed")
#Magic ("format=json")
YoutubeMetaData metaData (#Query (QUERY_VIDEO_URL) final String url);
}
Thanks.
Just put it right in the relative URL:
public interface YoutubeApi {
#GET("oembed?format=json")
YoutubeMetaData metaData(#Query(QUERY_VIDEO_URL) String url);
}
In kotlin you can specify the default parameter:
interface YoutubeApi {
#GET ("oembed")
suspend fun metaData (
#Query (QUERY_VIDEO_URL) url: String,
#Query(QUERY_FORMAT) alwaysJson: String = "json"
): Response<YoutubeMetaData>
}
Related
There is an interface for retrofit:
public interface GetDataService {
#GET("/news")
Call<ItemAPI> getAllItems();
}
How I can give parameters when I do the request? For example,
/news?id=1001
I think it must be looking like:
#GET("/news?id={id}")
But how do I do it correctly?
#GET("/v1/news_content")
Call<ItemPageAPI> getAllItems(#Query("id") String id);
#Query can add your parameters to the URL by itself.
public interface GetDataService {
#GET("/news?id={id}")
Call<ItemAPI> getAllItems(#Query("id") int id);
}
You can set parameters, headers in this way in the retrofit requests
#Headers("Accept: " + "application/json")
#GET(Constants.GET_PROPERTIES)
fun getFilteredProperties(#Query("access_token") access_token: String,
#Query("lat") lat: String,
#Query("long") long: String,
#Query("current_page") current_page: String,
#Query("location_name") location_name: String
): Call<GetPropertiesPojo>
Please use it like this:
#GET("/news?id={id}")
Call<ItemAPI> getAllItems(#Path("id") String idStr);
If the #Path annotation is not working then you can pass in the #Query annotation.
I have an url similar to http://www.myexample.com/rss/choose.php?type=level&id=2 and I want to use Retrofit in my android app. The problem is that type is a parameter which can take different values like level, grade, etc.
I can't adapt Retrofit every time type change because I get a "Annotations are not allowed here" in TestesInterface. I was looking to this post and applied to my app but it doesn't work.
static final String BASE_URL = "http://www.myexample.com/rss/";
public void start() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create()).build();
TestesInterface testesInterface = retrofit.create(TestesInterface.class);
Call<Channel> call = testesInterface.loadChannel("choose.php?type=level&id=2");
call.enqueue(this);
}
My interface:
public interface TestesInterface {
#GET
Call<Channel> loadChannel(#Url type);
}
This made that every time I want to change type to a different value I should change testesInterface.loadChannel("choose.php?type=level&id=2"). This doesn't work. Can you please help me?
After try different approaches I got the solution.
I had to use the tag #Query to type and id on interface and send the values when I'm invoking it in start():
#GET
Call<Channel> loadChannel(#Query("type") String type,
#Query("id") String value);
I just need to do a #GET for the following endpoint but not sure the syntax, here is my code:
public interface GithubService {
String SERVICE_ENDPOINT = "https://api.github.com";
String SERVICE_FUNDS_ENDPOINT = "http://iwg-testapi.azurewebsites.net";
// this works fine
#GET("/users/{login}")
Observable<Github> getUser(#Path("login") String login);
//here is the problem:
#GET("/stem/funds")
Observable<Funds> getFunds(#Path("funds") String login);
}
It's not a RxJava issue but a Retrofit.
I think the issue is on the GET annotation, as you want to use the path param.
#GET("/stem/{funds}") Observable<Funds> getFunds(#Path("funds")
(Notice that I add {} around funds because I want to use it at the path param)
You may want to check the Retrofit documentation.
I have a custom data class:
public static class Data {
...
}
I want to use this class in the URI of a resource in Jersey. For example:
#Path("test")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class ResourceTest {
#GET
#Path("/data-{data}")
public Response get(#PathParam("data") final Data data) {
...
}
}
Is this possible? I guess I need to inject some kind of converter, which converts the textual representation of a Data to a Data instance. I have been looking in the documentation, but haven't found something useful so far.
Ofcourse, I can change this to:
#Path("test")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class ResourceTest {
#GET
#Path("/data-{data}")
public Response get(#PathParam("data") final String input) {
final Data data = convert(input);
...
}
}
But I would rather do the conversion elsewhere/automagically wrt. the resource.
From the docs:
The type of the annotated parameter, field or property must either:
...
Have a constructor that accepts a single String argument.
Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String)).
Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance capable of a "from string" conversion for the type.
So if you provide a constructor Data(String) you should be fine.
I am using RESTEasy in my API development. My url is http://localhost:8080/project/player/M or http://localhost:8080/project/player
it means am pasing {gender} as path param.
my problem is how to mapp this url to REST method, i use below mapping
#GET
#Path("player/{gender}")
#Produces("application/json")
but if use it, it maps for http://localhost:8080/project/player/M but not for http://localhost:8080/project/player.
i need a regular expression to map zero or more path parameters
Thanks.
Is there any reason this must be a path parameter and not a query string ? If you change it to use the latter, then you can use a #DefaultValue annotation.
So your code would then look like the following:
#GET
#Path("player") //example: "/player?gender=F"
#Produces("application/json")
public Whatever myMethod(#QueryParam("gender") #DefaultValue("M") final String gender) {
// your implementation here
}
Path parameters (#PathParam) aren't optional. If you want to map;
http://localhost:8080/project/player/M
http://localhost:8080/project/player
You will need two methods. You can use method overloading;
#GET
#Path("player/{gender}")
#Produces("application/json")
public Whatever myMethod(#PathParam("gender") final String gender) {
// your implementation here
}
#GET
#Path("player")
#Produces("application/json")
public Whatever myMethod() {
return myMethod(null);
}
See the below link which has a sample of optional path parameters via regular expressions
RestEasy #Path Question with regular expression
You should use regex when you want have optional parameter in path.
So your code would then look like the following:
#GET
#Path("/player{gender : (/\\w+)?}")
#Produces("application/json;charset=UTF-8")
public Whatever myMethod(#QueryParam("gender") #DefaultValue("M") final String gender) {
// your implementation here
}
For more information see https://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/Using__Path_and__GET___POST__etc..html