Retrofit error-Missing either #GET URL or #Url parameter - android

I am working on Youtube API.
The base URL is <https://www.googleapis.com/youtube/v3/search/>
Request :GET
https://www.googleapis.com/youtube/v3/search?part=snippet&q={search_keyword}&key={API_KEY}
ApiService Interface code-
public interface ApiService {
#GET("")
Call<YoutubeResponse> searchVideos(#Query("part") String part,
#Query("q") String q,#Query("key") String apiKey);
}
The error: java.lang.IllegalArgumentException: Missing either #GET URL or #Url parameter.
in the line of code
Call<YoutubeResponse> call=service.searchVideos("snippet",s, URLConstants.Youtube_API_KEY);
I'm a beginner. Please help!

It's much more semantically correct to use https://www.googleapis.com/youtube/v3/ as your base URL and then declare #GET("search/") on your service method.
That said, if you really want your base URL to be the full path you can use #GET(".") to declare that your final URL is the same as your base URL.

Related

Retrofit 2 removes posrt after special character ":" from base Url

I have an Api https://hello.example.com:344/new/search/result.
Implementing same using Retrofit 2:
This is how initialising retrofit:
public static void initializeRetrofit() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://hello.example.com:344")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
service2 = retrofit.create(ContentService.class);
}
This is the interface request:
#POST("new/search/result")
Call<JsonObject> getSearchList(#Body JsonObject request);
But when i hit api : it removes the port from it and hits
"https://hello.example.com/new/search/result"
What is going wrong?
In your base url "https://hello.example.com:344" transform it to
"https://hello.example.com:344/"
There is no / (slash) in your base url as well as in the interface function. So the request becomes like "https://hello.example.com:344new/search/result " which will give u an error.
Add slash at the end of your base url like this "https://hello.example.com:344/"

What is the Interface in This URL in Retrofit

In Get Method:
http://xyz.erprnd.com/api/v1/customers/get?yearmonth=201807
My design interface:
#Headers({ "Content-Type: application/json;charset=UTF-8"})
#GET("customers/{get}/{yearmonthValue}")
Call<CustResponse> CustomerData(#HeaderMap Map<String, String> headers,
#Path("get") String get,
#Query("yearmonth") String yearmonth,
#Path("yearmonthValue") String yearmonthValue,
#Header("Authorization") String authHeader);
What can I change to make this work properly?
First, double check that you are passing "http://xyz.erprnd.com/api/v1" as your baseUrl when building Retrofit.
Then, base on the URL you posted, the interface should look something like:
#Headers({ "Content-Type: application/json;charset=UTF-8"})
#GET("customers/get")
Call<CustResponse> CustomerData(#HeaderMap Map<String, String> headers,
#Header("Authorization") String authHeader,
#Query("yearmonth") String yearmonth);
when you define query parameters, you just need to add the annotation #Query("param_name") and then the value of the variable passed in will be transform intro: param_name=variable_value
Also, I removed your #Path("get") parameter, since it seems that that part of the URL will always be /get and won't change.
Unfortunately, I cannot test it since I don't have authorization to your API.

What is the right way to handle Strings when doing HTTP requests?

I have an Android application acting as a client to my back end server.
I am doing a POST http request with a help of Retrofit lib with a String in the body.
Problem is, Retrofit is most likely escaping double quotes when using GSON builder.
That results in a field in my DB containing double quotes, example: "example_gcm_token".
I need to know whether I should handle that on server side or on client side and how to do that.
I assume it shouldn't be on the server side as it would mean I have to remove escaped quotes for every single endpoint.
#POST ("/Maguss/users/{userId}/gcmtoken")
Call<Void> setGcmToken(#Path("userId") Long userId, #Body StringEntity gcmToken);
I would try to replace the StringEntity with a POJO:
public class SetGcmTokenRequest {
#SerializedName("gcmtoken")
private String gcmToken;
public String getGcmToken() {
return gcmToken;
}
public void setGcmToken(String gcmToken) {
this.gcmToken = gcmToken;
}
}
And change the interface like this:
#POST ("/Maguss/users/{userId}/gcmtoken")
Call<Void> setGcmToken(#Path("userId") Long userId, #Body SetGcmTokenRequest setGcmTokenRequest);

Error in retrofit GET request

I am using retrofit for api calling in my android app. In one of my request url of request is like this:
/v1/employee/1?employeeId=50124
my retrofit method is:
#GET("/v1/xyz/{employeeId}?companyId={companyId}")
void getEmployee(#Path("employeeId")int employeeId, #Path("companyId") int companyId, Callback<List<Model>> callback);
but when i call api it throws error, please help how to append url like this in retrofit GET request.
For dynamic query parameters use #Query.
Try using the below code:
#GET("/v1/xyz/{employeeId}")
void getEmployee(#Path("employeeId")int employeeId, #Query("companyId")
int companyId, Callback<List<Model>> callback);

How #PUT some value in webservice via Retrofit

I wanna send a list of integer with userName and password to WebService some thing like bellow request
UpdateDocumentState(List<int> documentIds, string userName, string password)
But I don't know How to do that ? Use #Post Or #Put ? use #Query Or #Field ? I googled but didn't find any good example or tutorial which explained these well. ( All tutorial I found was about #GET )
could anyone give me some piece of code , how to do that ?
About the use of #PUT or #POST I think you had to get this information from the WebService developers.
Anyway, here sample code for both of Retrofit annotations with or without Callback response.
#POST("your_endpoint")
void postObject(#Body Object object, Callback<Response> callback);
#PUT("/{path}")
String foo(#Path("path") String thePath);
EDIT:
Object is a custom class which represent the data you had to send to the WebService.
public class DataToSend {
public List<Int> myList;
public String username;
public String password;
}
For example when the #POST annotation declaration will be:
#POST
void postList(#Body DataToSend dataToSend, Callback<Response> callback);
and then you call the method using Retrofit service
yourService.postList(myDataToSend, postCallback);

Categories

Resources