In my project, I have a Retrofit2 interface to define the URLs of some images on a server. I also have an OkHttp3 client which has a couple of interceptors.
Is there a way to get the full URL of one of those image (after the execution of the interceptors) so I can pass it to Picasso? I didn't find any method in Picasso that takes a Call directly.
Have you tried?
call.request().url() where call is type of retrofit2.Call
Alternatively
Let's say you have the following retrofit interface:
public interface ExampleService {
#GET("dummy/{examplePartialUrl}/")
Call<JsonObject> exampleList(#Path("examplePartialUrl") String examplePartialUrl;
}
With a call request:
Call<JsonObject> mCall = dummyService.exampleList("partialDummy")
To get the full URL use:
dummyService.exampleList("partialDummy").request().url().toString()
Source: Retrofit 2 check call URL
Related
I'm using a get request to get weather data using api from weatherbit.io. How do I use the base Url and the relative url correctly.
I've tried to use base url inside my main activity and the relative url inside the interface I've created.But I'm not getting any output in the textview
My main activity:
Retrofit retrofit=new Retrofit.Builder().baseUrl("https://api.weatherbit.io/v2.0/").addConverterFactory(GsonConverterFactory.create()).build();
OpenWeatherMapApi openWeatherMapApi=retrofit.create(OpenWeatherMapApi.class);
Call<List<Post>> call=openWeatherMapApi.getPosts("my_city_name","my_country_name","my_key");
my retrofit interface:
#GET("forecast/daily")
Call<List<Post>> getPosts(#Query("city")String city,#Query("country")String country,#Query("key")String key);
I have a URL
http://ec2-13-58-192-11.us-east-2.compute.amazonaws.com:3000/api/FCLContainer?filter=%7B%22where%22%3A%20%7B%22carrier%22%3A%20%22resource%3Aorg.shipping.bitnautic.Carrier%23carrier0%40carrier.com%22%7D%7D
But when I print URL in my logcat window after passing in retrofit and call the method call.request().url()
It prints this
http://ec2-13-58-192-11.us-east-2.compute.amazonaws.com:3000/api/FCLContainer?filter=%257B%2522where%2522%253A%2520%257B%2522carrier%2522%253A%2520%2522resource%253Aorg.shipping.bitnautic.Carrier%2523carrier0%2540carrier.com%2522%257D%257D%0A%0A
How can I handle this url?
It seems you are giving encoded url to retrofit ,hence it reencoding url and showing you different .
Set url in Retrofit as Simple String ,in your case
http://ec2-13-58-192-11.us-east-2.compute.amazonaws.com:3000/api/FCLContainer?filter={"where": {"carrier": "resource:org.shipping.bitnautic.Carrier#carrier0#carrier.com"}}
In Retrofit log you will see as endcoded url
http://ec2-13-58-192-11.us-east-2.compute.amazonaws.com:3000/api/FCLContainer?filter=%7B%22where%22%3A%20%7B%22carrier%22%3A%20%22resource%3Aorg.shipping.bitnautic.Carrier%23carrier0%40carrier.com%22%7D%7D
I am using an API that is out of my control, as well having joined a development team recently that is using Retrofit1.
I am unable to send the request to the server in the format required, as the server requires multipart form data Body in the following format:
Uniqueidentifier:FileName.jpg:ReroutServerIP:Base64EncodedDocString.
I have tried many different techniques in order to accomplish this task but I cannot find any working method to do this. The server tells me that the message format not supported. Here is my current code (with the url stripped out). Please could someone assist?
#POST("URL")
public Response post_SendData(#Header("Content-Type") String ContentType, #Body String body);
In order to achieve the desired result, I can use postman with no headers and post a file from my system using the form-data post method. In the working postman post, the Key is the formatted string mentioned above and the value is a file selected from my desktop. Please see below for postman (edited to remove urls).
Postman
Thanks a lot guys.
If you want to send a file to the server :
public void uploadPictureRetrofit(File file, Callback<YourObject> response) {
// this will build full path of API url where we want to send data.
RestAdapter restAdapter = new RestAdapter
.Builder()
.setEndpoint(YOUR_BASE_URL)
.setConverter(new SimpleXMLConverter()) // if the response is an xml
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
// SubmitAPI is name of our interface which will send data to server.
SendMediaApiInterface api = restAdapter.
create(SendMediaApiInterface.class);
TypedFile typedFile = new TypedFile(MULTIPART_FORM_DATA, file);
api.sendImage(typedFile, response);
}
And this is the interface SendMediaApiInterface :
public interface SendMediaApiInterface {
#Multipart
#POST("url")
void sendImage(#Part("here_is_the_attribute_name_in_webservice") TypedFile attachments, Callback<YourObject> response);}
I have a requirement to get a request body and to perform some logic operations with Retrofit 2.0 before doing enque operation. But unfortunately I am not able to get the post body content from my service call. At present after searching a lot I found only one solution like logging the request that I am posting using Retrofit 2.0 from this method by using HttpLoggingInterceptor with OkHttpClient. I am using the following code to log the request body in the Android Logcat:
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY);
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(apiClass);
Problem I am facing with the above method:
Request body are seen only on the default Android Logcat like
02-04 01:35:59.235 5007-5035/com.example.retrofitdemo D/OkHttp:{"nameValuePairs":{"id":"1","name":"chandru","type":"user"}}
But the above method returns only response body in the logcat, I am not able to get it as either String or JSONObject. Eventhough if I could able to get the response body using HttpLoggingInterceptor, my request body will be shown in the Logcat with the tag "OkHttp" all the time even after the application goes into the production (So primarily this leads like a kind of relieving the post data in the logcat).
My Requirement:
I need to get the request body as String or JSONObject or whatever method without reviling the post data in the Logcat.
What I tried:
I tried to fetch the request body even after onResponse from Response<T> response, but though I couldn't able to get it done possible. Please find the code that I am using for this purpose as follows:
Gson gson = new Gson();
String responseString = gson.toJson(response.raw().request().body());
Note: The above converted request body using gson.toJson method returns only the meta data not the request post data.
Kindly help me with this by providing your valuable tips and solutions. I have no idea how to do this. I am completely stuck up with this for the past two days. Please forgive if my question is too lengthy. Your comments are always welcome. Do let me know if my requirement is not clear. Thanks in advance.
I have got it working from this link
Retrofit2: Modifying request body in OkHttp Interceptor
private String bodyToString(final RequestBody request) {
try {
final RequestBody copy = request;
final Buffer buffer = new Buffer();
if (copy != null)
copy.writeTo(buffer);
else
return "";
return buffer.readUtf8();
} catch (final IOException e) {
return "did not work";
}
}
Retrofit dependencies i am using are
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
I too had the similar issue, I had set JSON as #Body type in retrofit, so the namevaluepair appears in front of the raw body, and it can be seen inside the interceptor.
Even though if we log/debug the jsonObject.toString we see the request as correct without the namevaluepair presented.
What i had done was by setting
Call<ResponseBody> getResults(#Body JsonObject variable);
and in the calling of the method i converted the JSONObject to JsonObject by
new JsonParser().parse(model).getAsJsonObject();
i'm trying to call an api in my application
i've the following url template
test-test.domainname.com/feeds/json/v3/attribute/attribute
i'm using retrofit 2
but i get the following fatal exception
Illegal URL: test-test.domainname.com
and this is my interface
public interface Iinterface{
#GET("feeds/json/v3/attribute/"+attribute)
Call<ArrayList<result>>getresult();
}
can someone help me with this problem ...
my base URL is here: http://myapiname.azurewebservices.net
and feed method is like that :
public interface Iinterface{
#GET("/feeds/json/v3/attribute/"+attribute)
Call<ArrayList<result>>getresult();
}
And working perfectly. Please add http or https and try again
You do not have a protocol section. Prepend http:// or https:// depending on which applies to your url --
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://test-test.domainname.com")
// ... other retrofit options
.build();
In my case,
my base url contained space character. (eg. http://myapiname.azure webservices.net )
I fixed this Error by removing space in my base URL.
Illegal URL Exception in retrofit is triggered when your passed url is
not really existed or not fix with url standard.
By mistake, I provided empty string to baseUrl() so because of this i was getting java.lang.IllegalArgumentException: Illegal URL exception.