我在http://192.168.1.12:8080/api/hooray的服务器上提供了这项简单的服务:
router.route('/hooray/')
.get(function (req, res) {
res.status(200).send({ result: true, message: "hooray" })
})
它确实可以使用Postman,但是从我的Android应用程序中我无法访问该URL,我不知道为什么。
这是(简化)代码:
public interface HoorayAPI {
@GET("hooray/")
Observable<HoorayResponse> hooray();
}
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 ...
}
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!!!");
}
});
}
当我调用foo()方法时,我收到此错误:
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)
我无法理解,因为错误: HTTP 404 Not Found 。该服务是可以访问的并且有效。我使用POST方法以与HoorayAPI非常类似的方式为 login 编写了一个服务API,它可以工作。为什么它在这里不起作用?我的意思是,代码似乎是正确的。
答案 0 :(得分:2)
好的,我找到了答案。问题发生在.baseUrl(getResources().getString(R.string.url))
。该URL作为
<string name="url">http://192.168.1.12:8080/api/</string>
但是,在另一个资源文件 values- it /string.xml 中有以下内容:
<string name="url">https://myapp-api.mydomain.io/api</string>
由于智能手机的语言设置为意大利语,因此使用了资源文件 values-it / string.xml ,因此程序识别出 https://myapp-api.mydomain.io/api 作为网址而不是http://192.168.1.12:8080/api/。
请注意正确更新资源文件中的所有网址以避免此问题。
答案 1 :(得分:0)
您确定手机是否连接到同一个局域网?
否则你可以尝试删除界面上的结束斜杠,但这不应该是问题
public interface HoorayAPI {
@GET("hooray")
Observable<HoorayResponse> hooray();
}
答案 2 :(得分:0)
您应该将subscribeOn(Schedulers.newThread())
的类型更改为此.subscribeOn(Schedulers.single())
这对我有帮助=)