I'm planning to replace Apache HTTP client with retrofit in my project.
The problem I'm facing is that retrofit didn't(I couldn't find) support setting HTTP method to request at runtime.
In my Web Service I don't know what HTTP method to call in advance, so annotations #GET, #POST, ... are useless.
Retrofit is not designed for dynamic url requests. You configure retrofit with your API base URL then make specific requests.
For a more flexible dynamic option use out OkHttp. It is the HTTP Client used by Retrofit and it easy to work with.
You can use Retrofit 2 for dynamic URL request with the new #Url annotation:
public interface CarService {
#GET
public Call<ImageResponse> getPicture(#Url String url);
}
Then just also create #POST, #PUT etc. You are going to have to make the choice somewhere.
Related
I'm currently building a Retrofit object as follows:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
I don't need to pass it a client, i.e. no need to deal with the internals.
Now I would like to set a timeout, but all the examples I found involve creating an okHttpClient and assigning it to the Retrofit object.
Does it mean that okHttp is the de-facto client used by the system in all the situations?
Does it mean that okHttp is the de-facto client used by the system in all the situations?
Yes. Retrofit uses OkHttp for its network I/O. For example, in the Retrofit documentation, they mention in the section on R8/Proguard:
You might also need rules for OkHttp and Okio which are dependencies of this library
I want to build an api for an android studio application to make the base url of the retrofit. I am having problems to make the base_url. I want to know how I should make the base_url of the retrofit. I am having problems validating the base_url. I want to know the procedure to make a base_url for retrofit.
If this is my url
http://api.themoviedb.org/3/movie/top_rated?api_key=12345678910111213
Then base url becomes
http://api.themoviedb.org/
It will be passed in as
retrofit = new Retrofit.Builder()
.baseUrl("http://api.themoviedb.org/");
and the remaining part will become get or post query
I have this request on Postman :
How can i make this request on Android?
You ca use very convenient service jsonschematopojo.org which will create java objects feom your Rest response and retrofit library. See post How to consume this JSON structure via Retrofit 2?
Netcipher is an Android Library Project that provides multiple means to improve network security in mobile applications. The “Onion” name refers to not only the Onion Routing concept used by Tor (which provides anonymity and resistance to traffic surveillance), but also the idea of multiple layers of security that any application should utilize.
More specifically this library provides:
1. Stronger Sockets: Through support for the right cipher suites, pinning and more, we ensure your encrypted connections are as strong as possible.
2. Proxied Connection Support: HTTP and SOCKS proxy connection support for HTTP and HTTP/S traffic through specific configuration of the Apache HTTPClient library
https://guardianproject.info/code/netcipher/
You need to implement your own Client that will execute Retrofit request on Netcipher http client.
Translate Request to appropriate Netcipher request (copy http method, headers, body)
Execute the translated request on Netcipher http client
Obtain response and translated it to retrofit Response (copy http status code, response, headers)
Return response to be deserialized to type.
Pass your Client to RestAdapter.Builder.
Done.
public class NetcipherClient implements Client{
private Context mContext;
public NetcipherClient(Context context){
mContext = context;
//As far as I could see from the sample, Netcipher seems to be dependant on application `Context`.
}
#Override
public retrofit.client.Response execute(retrofit.client.Request request) throws IOException {
//Set up configuration for Netcipher (proxy, timeout etc)
// Translate Request to Netcipher request
// Execute and obtain the response
// Build Response from response
return response;
}
}
We've recently implemented support for OkHTTP in NetCipher so it should be easy to add Tor support to Retrofit via OkHTTP. Here's how:
compile 'info.guardianproject.netcipher:netcipher-okhttp3:2.0.0-alpha1'
The core of this is just running the method to set things up:
StrongOkHttpClientBuilder
.forMaxSecurity(this)
.withTorValidation()
.build(this);
See the included sample-okhttp3 project for a full example, which is part of the git repo https://github.com/guardianproject/NetCipher
Except Tor-like routing OkHttp also have the certificate pinning ability & proxy support. And it works with Retrofit out-of-the-box! So, if Tor-like functions are not so important for you, I recommend to utilize OkHttp's awesomeness. Otherwise, answer by #NikolaDespotoski is your perfect choice.
I'm working on REST API client for Android using Retrofit.
Some of the use something like this http://my.backend.com and others use https://my.backend.com. The way I found is to create two separate interfaces and build two RestAdapters with different endpoints.
But I would like to keep my interfaces consitent and I'm wondering if it is possible for example build my Res adapter with my.backend.com and specify if the methot thould use https with #HTTPS annotation ?
Thanks.
The only thing you can change on a RestAdapter after it's been built is the log level so I'm afraid the only solution is to have two RestAdapters. Two seperate interfaces should not be necessary though, as long as the path after your endpoint (my.backend.com) is the same for both the http and the https version.
You can do the following generic method which returns retrofit and keep just one interface. "baseUrl" can be either "http" or "https" urls.
public static Retrofit getRetrofit(#NotNull String baseUrl) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}