java.lang.IllegalArgumentException: Illegal URL with retrofit - android

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.

Related

When I print URL using retrofit call.request().url() it prints different URL

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

Get final request URL without executing the request in Retrofit2/OkHttp3

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

Can retrofit get json file from assets folder?

Code
public interface LocalApi {
String HOST = "file:///android_asset/";
#GET("{filename}")
Flowable<XXXXBean> getLocalData(#Path("filename") String filename);
}
but I get a NullPointExpection, why?
Internally Retrofit uses OkHttp's HttpUrl class to figure out what the actual URL will be. This class is designed to only work for the http:// and https:// schemes. This means that it cannot retrieve local files and will either throw an Exception or produce a result you don't expect.

How to detect a redirection in Glide v4 / OkHttp3?

Using Glide v4 and OkHttp3, how can I detect a redirection and load another url when it happens?
My usecase: I use the Glide v4 library with OkHttp3 to download pictures in my app. Sometimes when a picture is not available, a redirection is performed by the server to provide another picture instead of the one I originaly wanted. I can see it in my browser because when I request url A, I finally land on url B with the second picture. When that happens I want to instead load url C that is derived from url A (so not a static url).
At the moment I can detect the redirection using an OkHttp3 Interceptor:
public Response intercept(#NonNull Chain chain) throws IOException {
String requestUrl = chain.request().url().toString();
Response response = chain.proceed(chain.request());
String responseUrl = response.request().url().toString();
boolean redirect = !requestUrl.equals(responseUrl);
if (redirect) {
Timber.d("Detected redirection");
}
return response;
}
but then I don't know how to cleanly load url C. I don't see how I can load another url in the interceptor, and if I throw an IOException to handle the error later in a Glide RequestListener it will just result in a GlideException so I can't determine why it was throw.
OkHttp should be automatically redirecting by default and loading the final content. See OkHttpClient.Builder.followRedirects.
https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.Builder.html#followRedirects-boolean-
My own testing suggests this is working, e.g.
$ oksocial 'https://httpbin.org/redirect-to?url=https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg'
For detecting it, I assume your interceptor is registered via addInterceptor instead of addNetworkInterceptor. Is that correct?
It is also worth ensuring the redirect is via a 30X response and not via a HTML refresh.
The solution I ended up with is pretty ugly: the request interceptor I showed in the question raise an IOException when a redirection is detected. I will then use a GlideListener to detect the error, and load the url C. The ugly part is that in the GlideListener I can't determine the cause of the error, it can be a redirection but it can also be a network error or anything else.
A cleaner version of this solution can probably be achieved with a custom OkHttpUrlLoader but it's too much code for my simple usecase.
This is my solution in Glide v4 / OkHttp3
String redirect = response.header("location");
if(redirect != null && !request.url().toString().equals(redirect)) {
// if redirected your code here
}

Android Volley MalformedURLException Bad URL

After making a second network request using Volley, I always get this error. It doesn't seem to matter what the url I put in is. Volley always claims it is malformed.
08-04 20:16:26.885 14453-14470/com.thredup.android E/Volley﹕ [994] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL
java.lang.RuntimeException: Bad URL
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:127)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
Caused by: java.net.MalformedURLException: Protocol not found:
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
Investigating further, I put a couple logs in HurlStack. In
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders),
the request that fails is REQUEST [ ] 0x0 LOW 26."
Thus, line 101 of HurlStack : URL parsedUrl = new URL(url);
fails with an empty url (request.getUrl() is empty).
I am using OkHttpStack (extending HurlStack).
Any ideas on what could be causing this?
actually the problem is with your url not with the volley. Your Url is not a URI. There is no protocol component in it. It needs http:// or whatever other protocol you intend. If you have the http in your url make sure where it is correctly formed or not.
For example your url formation should be like this
public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
Don’t forget to read the URL Specification and make sure the URL you are providing is valid.
Make Sure that you have passed the URL as the second parameter in JsonObjectRequest or StringRequest. I made the same mistake which produced the same error like what you faced.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, URL, null, ResponseListener, ErrorListener);
Use
http://
OR
https://
prefix to your URL
example:
example.com/information.json
write it as
http://example.com/information.json
this Exception occur while you are hitting an Url that is not prefixed withhttp// or https//.so check there is therehttp// is with your URL.
you can get more information here and see these links

Categories

Resources