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.