We're using retrofit android client (http://square.github.io/retrofit/) for network calls to our server and I cant figure out how to access values in the strings.xml file from the interface class that we created for retrofit.
Here's a small sample of our interface code.
//all retrofit imports
public interface APIService {
String API_KEY = "XXXX"; //Get this value from strings.xml
#GET("/api/getNames?api_key=" + API_KEY)
public ArrayList<String> getNames();
}
Right now XXXX is hardcoded but we want it to get taken from strings.xml.
Thank you.
You can pass the API_KEY as a parameter using
#GET("/api/getNames")
public ArrayList<String> getNames(#Query("api_key") String apiKey);
Check URL manipulation section URL Manipulation - Retrofit
Related
I am trying to hide my API Key and therefore am setting up my Retrofit Api Interface as such:
public interface ApiInterface {
#GET("?api_key=" + popularmovies.name.com.popularmovies.BuildConfig.TMD_API_KEY + "&language=en-US")
Call<Movies> getImages();
}
However, I am receiving an error "Attribute value must be a constant." Is there a way to resolve and access this String?
UPDATE: Here is a second try:
private static final String API_KEY = popularmovies.troychuinard.com.popularmovies.BuildConfig.TMD_API_KEY;
public interface ApiInterface {
#GET("?language=en-US")
Call<Movies> getImages(#Query("api_key") API_KEY);
}
UPDATE: Here is a third try:
Below is the logging I am seeing as the URL making the request, as can be seen the API Key is now at the end of the URL which is incorrect syntax:
06-10 18:41:33.212 4969-4993/? D/OkHttp: <-- 401 Unauthorized http://api.themoviedb.org/3/movie/popular?language=en-US&api_key=?api_key%3D09b0a9a9d5d9ddee2b3bc69e78b02457 (592ms)
Access-Control-Allow-Origin: *
Use:
#GET("/rest/of/your/path/here?language=en-US")
Call<Movies> getImages(#Query("api_key") apiKey);
where you pass in popularmovies.name.com.popularmovies.BuildConfig.TMD_API_KEY to getImages():
Call<Movies> call=yourRetrofitInterface.getImages(BuildConfig.TMD_API_KEY);
Also, your #GET annotation seems to be missing the path to the REST endpoint (shown in my sample as /rest/of/your/path/here).
I'm trying to upload a file to Amazon's S3 using a pre-signed URL. I get the URL from a server which generates the URL & sends it to me as part of a JSON object. I get the URL as a String, something like this:
https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/ImageName?X-Amz-Security-Token=xxfooxx%2F%2F%2F%2F%2F%2F%2F%2F%2F%2Fxxbarxx%3D&X-Amz-Algorithm=xxAlgoxx&X-Amz-Date=20170831T090152Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=xxcredxx&X-Amz-Signature=xxsignxx
Unfortunately, when I pass this to Retrofit2, it modifies the String attempting to make it into a URL. I've set encoding=true which took care of most of the problem but not completely. I know the String works as it is. I've tried it in Postman & get a successful response.
1st I tried just putting the String (except for what I cut out as baseUrl) as a whole into the Path
public interface UpdateImageInterface {
#PUT("{url}")
Call<Void> updateImage(#Path(value="url", encoded=true) String url, Body RequestBody image);
}
The calling code:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/")
.build();
UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class);
// imageUrl is "ImageName..."
Call<Void> call = imageInterface.updateImage(imageUrl, requestFile);
This works mostly except the the '?' (after "ImageName") get converted to "%3F". This causes a Bad Request / 400.
My next attempt was to create a query with Retrofit2 but then dump the whole String (with multiple queries) into the query.
public interface UpdateImageInterface {
#PUT("ImageName")
Call<Void> updateProfilePhoto(#Query(value="X-Amz-Security-Token", encoded = true) String token, #Body RequestBody image);
}
The calling code:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://com-example-mysite.s3-us-east-1.amazonaws.com/userFolder/")
.build();
UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class);
// imageUrl is "xxfooxx..."
Call<Void> call = imageInterface.updateImage(imageUrl, requestFile);
This gets the '?' rendered correctly but all of the '&' get changed to "%26"
Lastly I tried passing the whole String in baseUrl() but that gives an IllegalArgumentException for not having '/' on the end.
I know that I could parse the pre-signed URL to make multiple queries & assemble them in Retrofit2 as queries should be done but I'd like to avoid that processing.
To restate the question:
Is there a way to easily (without heavy String parsing) upload a file to S3 with a pre-signed URL using Retrofit2?
With help from a colleague, this is the solution.
public interface UpdateImageInterface {
#PUT
Call<Void> updateImage(#Url String url, #Body RequestBody image);
}
Calling code:
String CONTENT_IMAGE = "image/jpeg";
File file = new File(localPhotoPath); // create new file on device
RequestBody requestFile = RequestBody.create(MediaType.parse(CONTENT_IMAGE), file);
/* since the pre-signed URL from S3 contains a host, this dummy URL will
* be replaced completely by the pre-signed URL. (I'm using baseURl(String) here
* but see baseUrl(okhttp3.HttpUrl) in Javadoc for how base URLs are handled
*/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.dummy.com/")
.build();
UpdateImageInterface imageInterface = retrofit.create(UpdateImageInterface.class);
// imageUrl is the String as received from AWS S3
Call<Void> call = imageInterface.updateImage(imageUrl, requestFile);
Javadoc for info on #Url (class Url) &
baseUrl() (class Retrofit.Builder)
MediaType is a class in the OkHttp library that is often used with Retrofit (both from Square). Info about constants passed to the parse method can be found in the Javadoc.
Use the following while uploading directly to S3 using presigned URL.
#Multipart
#PUT
#Headers("x-amz-acl:public-read")
Call<Void> uploadFile(#Url String url, #Header("Content-Type") String contentType, #Part MultipartBody.Part part);
How do I pass Dynamic JSON file name using Retrofit
MainActivity.java:
final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://www.domain.com/").build();
api flowerapi = restadapter.create(api.class);
api.java:
public interface api {
#GET("/JSONS/flowers.json")
void getData(Callback<List<Flower>> response);
}
As you can see in my above code, I am using only single/one and only flowers.json
What If I would like to call dynamic json files based on some conditions, like in some cases I have to call flowers.json, in some cases roses.json and in some cases something.json
with the #Path annotation
#GET("/JSONS/{name}")
void getData(#Path("name") String name, Callback<List<Flower>> response);
I have one property in property file
appointments.deleteAppointmentwithReasonApi=api/appointment/{id}?reason={reason}
URL=http://xyz/etc/
in another file
public static final String DELETE_APPOINTMENT_REASON = PropertiesUtil.getPropertyValueFromKey(REST_WEBSERVICE_URLS_PROP_FILE,
"appointments.deleteAppointmentwithReasonApi"); // To get API name
public static final String URL = ServicesUtil.getURL(); // to get endpoint URL
In my java API call, I gave something like this
WebTarget target = client.target(CommonConstants.URL)
.path(CommonConstants.DELETE_APPOINTMENT_REASON)
.resolveTemplate("id", appointmentID).resolveTemplate("reason", reason);
System.out.println(target);
My response is printing like this...
JerseyWebTarget { http://xyz/etc/api/appointment/abc-123-ced-456%3Freason=Test }
which is not hitting the proper Web Services...I want it to be like this
JerseyWebTarget { http://xyz/etc/api/appointment/abc-123-ced-456?reason=Test }
I know i need to encode URL. I am not able to do it somehow. Any suggestion ?
I'm trying to pass a string of the format below as the body of a http post request.
param1=PARAM1¶m2=PARAM2¶m3=PARAM3
But retrofit encodes my body so that = becomes \u003d and & becomes \u0026. And I end up with a string which actually looks like this:
param1\u003dPARAM1\u0026param2\u003dPARAM2\u0026param3\u003dPARAM3
How can I prevent that?
My retrofit rest api is defined as follows.
public interface RestAPI {
#POST("/oauth/token")
public void getAccessToken(#Body String requestBody, Callback<Response> response);
}
If you have a serialized class (like a HashMap) in the request body and you want to prevent encoding that (like in vezikon's and my problem), you can create a custom Gson with disabled escaping using:
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Pass this converter to your rest adapter:
yourRestAdapter = new RestAdapter.Builder()
.setEndpoint(.....)
.setClient(.....)
.setConverter(new GsonConverter(gson))
.build();
This way the "=" characters in the post body stay intact while submitting.
To answer the question directly, you can use TypedString as the method parameter type. The reason the value is being changed is because Retrofit is handing the String to Gson in order to encode as JSON. Using TypedString or any TypedOutput subclass will prevent this behavior, basically telling Retrofit you will handle creating the direct request body yourself.
However, that format of payload is called form URL encoding. Retrofit has native support for it. Your method declaration should actually look like this:
#FormUrlEncoded
#POST("/oauth/token")
void getAccessToken(
#Field("param1") String param1,
#Field("param2") String param2,
#Field("param3") String param3,
Callback<Response> callback);
Using Kotlin
For Retrofit 2 you can initialize retrofit with a Gson converter factory.
val builder = GsonBuilder().disableHtmlEscaping().create()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(builder))
.client(monoOkHttpClient())
.build()
This builder should remove escaping from your json output.
Gradle file dependencies:
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
This issue can be fixed with below workaround.
#POST("yourString")
Call<YourResponseModel> yourCallMethod(#Query("yourKey") String yourValue,
#Query("yourKey") String yourValue,
#Query("yourKey") String yourValue);
Note : Don't use "#FormUrlEncoded" for this case.
Reference Here - https://github.com/square/retrofit/issues/1407