Retrofit2: HTTP 404 Not Found
Ok, I've found the answer. The problem was in .baseUrl(getResources().getString(R.string.url))
. The url was stored in the resource file values/string.xml as
<string name="url">http://192.168.1.12:8080/api/</string>
However, in another resource file values-it/string.xml there was the following:
<string name="url">https://myapp-api.mydomain.io/api</string>
Because of the language of my smartphone was set to italian, the resource file values-it/string.xml was used, so the program recognized https://myapp-api.mydomain.io/api as url instead of http://192.168.1.12:8080/api/.
Be aware to correctly update all the url in the resource files to avoid this problem.
Related videos on Youtube
s.dallapalma
I am proactive, self-studier, with a strong can-do attitude person motivated by an inexhaustible enthusiasm and commitment and excited to follow-up collaborations with industries working in the Software Engineering domain. Am intrigued and inclined towards areas of Artificial Intelligence applied to Software Engineering, Machine Learning, and Recommendation Systems, with with a particular focus on software defect prediction, code smells detection, and software quality assurance in general. I’m currently working on defect prediction of Infrastructure as Code, i.e., machine-readable, configuration code that is used to manage and provide services in cloud applications.
Updated on June 04, 2022Comments
-
s.dallapalma almost 2 years
I have this simple service on my server at http://192.168.1.12:8080/api/hooray:
router.route('/hooray/') .get(function (req, res) { res.status(200).send({ result: true, message: "hooray" }) })
It does work from Postman, but from my Android application I unable to reach that url, and I don't know why.
That's the (simplified) code:
Service's Interface
public interface HoorayAPI { @GET("hooray/") Observable<HoorayResponse> hooray(); }
My custom Response class: HoorayResponse
public class HoorayResponse{ @SerializedName("result") @Expose private boolean result; @SerializedName("message") @Expose private String message; public HoorayResponse(boolean result, String message) { this.result = result; this.message = message; } // Getter and setter ... }
Caller
public void foo(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.1.12:8080/api/") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); HoorayAPI service = retrofit.create(HoorayAPI.class); Observable<HoorayResponse> hoorayObservable = service.hooray(); hoorayObservable .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<HoorayResponse>() { @Override public void onCompleted() {} @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(HoorayResponse response) { System.out.println("Hooray!!!"); } }); }
When I call the foo() method, I get this error:
retrofit2.adapter.rxjava.HttpException: HTTP 404 Not Found at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:43) at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173) at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80) at rx.Subscriber.setProducer(Subscriber.java:211) at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76) at rx.Subscriber.setProducer(Subscriber.java:205) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152) at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.unsafeSubscribe(Observable.java:8460) at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:776)
I can't figure out because the error: HTTP 404 Not Found. That service is reachable and it works. I wrote a service API for login in a very similar way to HoorayAPI, with a POST method, and it works. Why it doesn't work here? I mean, the code seems to be correct.
-
s.dallapalma over 6 yearsYes, the mobile phone is connected to the same LAN, and no, the slash is not the problem :/. As I said, with another service (login) it does work, so this behavior is weird.
-
Andrea Vassallo over 6 yearsCan I try the service ? If you use ngrok to share your local WS I can try to debug with you.
-
Andrea Vassallo over 6 yearsJust a curiosity! Have you the same error if you use ngrok url ?
-
Andrea Vassallo over 6 yearsSo I can't debug it. It is very strange, try to reboot the WS! If I get any idea I'll write to you! If you can solve, write the answer please :)
-
s.dallapalma over 6 yearsok, thank you. I really appreciate it. At least it works on ngrok, for the moment ;P
-
s.dallapalma over 6 yearsI post the answer, I don't know why this behavior. Thank you,if I did not try it with ngrok I would not have been able to solve the problem
-
Andrea Vassallo over 6 yearsIt was a pleasure. However, I use this to help me in debugging: stackoverflow.com/a/33256827/5650222 Try it :)
-
William Reed over 5 yearsThis should actually be subscribing on
Schedulers.io()
as this is a blocking operation