我正在编写一个 Android 应用程序。
交互方式:REST API。
服务器:WampServer 本地服务器
问题:没有连接。我正在尝试连接到 API,但它不起作用:
编码:
public class ApiUtils {
// private static String apiUrl = "https://localhost/usatu/";
// private static String apiUrl = "https://192.168.0.102/usatu/";
// private static String apiUrl = "https://192.168.0.1/usatu/";
private static String apiUrl = "https://10.0.3.2/"
private static Retrofit retrofit;
private static APIService api;
public static Retrofit getRetrofit(){
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(apiUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return retrofit;
}
public static APIService getApi(){
if(api == null){
api = getRetrofit().create(APIService.class);
}
return api;
}
}
我根本无法连接到服务器:回溯:
W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Failed to connect to /192.168.0.1:443
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.net.ConnectException: Failed to connect to /192.168.0.1:443
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:248)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:166)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall.execute(RealCall.java:92)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:186)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
at io.reactivex.Observable.subscribe(Observable.java:10955)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10955)
at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
W/System.err: at io.reactivex.Single.subscribe(Single.java:2779)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err: at java.lang.Thread.run(Thread.java:764)
Caused by: java.net.ConnectException: failed to connect to /192.168.0.1 (port 443) from /192.168.234.231 (port 55154) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:273)
at libcore.io.IoBridge.connectErrno(IoBridge.java:188)
at libcore.io.IoBridge.connect(IoBridge.java:130)
W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:616)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:73)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:246)
... 33 more
W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:262)
... 43 more
虽然如果我通过浏览器访问服务器,http://localhost/usatu/v1/semesters会返回正确的结果。
ip 使用 ipconfig (window) 命令确定
在 Genymotion 模拟器上测试
更新:通过模拟器上的浏览器,我连接到地址htttp://10.0.3.2/v1/semesters
并且它在那里工作,但应用程序中的错误并没有消失
接口:
public interface APIService {
@GET("v1/semesters")
Single<List<Semester>> getSemesters();
}
主持人:
@InjectViewState
public class SearchScheduleStudentPresenter extends BasePresenter<SearchScheduleStudentView> {
private SearchScheduleStudentView mView;
public SearchScheduleStudentPresenter(SearchScheduleStudentView view) {
mView = view;
}
public void getSemesters(){
mCompositeDisposable.add(
ApiUtils.getApi()
.getSemesters()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
response -> mView.showSemesters(response)
)
);
}
}
看法
public interface SearchScheduleStudentView extends BaseView {
void showSemesters(List<Semester> semesters);
}
分段:
@Override
public void showSemesters(List<Semester> semesters) {
List<String> semestersName = new ArrayList<>();
for (Semester semester : semesters) {
semestersName.add(semester.getName());
}
// adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, semestersName);
}
显现
<uses-permission android:name="android.permission.INTERNET"/>


看起来您正在尝试使用路由器的 IP 进行连接,而不是您的计算机。尝试取消注释地址
192.168.0.102。您可以阅读更多关于解释命令输出的信息
ipconfig,例如,这里。错误是我指定了 https 而不是 http。
您可以使用网络上计算机的 ip(在我的情况下,它是 192.168.0.102)或 10.0.3.2(永久),从 2.2 版开始添加到 Genymotion