gpt4 book ai didi

android - 改造 |句柄 "server not found exception"

转载 作者:行者123 更新时间:2023-11-29 17:49:41 25 4
gpt4 key购买 nike

当尝试从 Android 应用程序访问 RESTful api 时,您需要检查两件事:

  1. 用户的 wifi 已启用(并且已建立连接)
  2. 您的应用实际上可以访问服务器。 (我的服务器在本地网络上)

我已经设法检查了第一个,但我不知道如何检查最新的。

这是我的输出

05-25 15:49:46.929  15485-15999/com.tesis.restapp.restapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: retrofit.RetrofitError: java.net.ConnectException: failed to connect to /192.168.1.25 (port 8080) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:385)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:241)
at $Proxy0.logIn(Native Method)
at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:83)
at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:70)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
Caused by: java.net.ConnectException: failed to connect to /192.168.1.25 (port 8080) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:220)
at libcore.io.IoBridge.connectErrno(IoBridge.java:158)
at libcore.io.IoBridge.connect(IoBridge.java:118)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:849)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:66)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:322)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:241)
            at $Proxy0.logIn(Native Method)
            at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:83)
            at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:70)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:207)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:158)
            at libcore.io.IoBridge.connect(IoBridge.java:118)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
            at java.net.Socket.connect(Socket.java:849)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
            at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
            at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
            at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:66)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:322)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:241)
            at $Proxy0.logIn(Native Method)
            at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:83)
            at com.tesis.restapp.restapp.activities.intro.IntroActivity$SignIn.doInBackground(IntroActivity.java:70)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

编辑

这是我的界面(目前,它只是尝试让用户登录)

public interface RestAppApiInterface {

@FormUrlEncoded
@POST(Constants.URL_LOGIN)
public User logIn(@Field("username") String username, @Field("password") String password);

}

此类返回接口(interface)的一个实例:

public class ApiClient {

private static final String API_URL = "http://192.168.1.25:8080";

private static RestAppApiInterface sRestAppService;

public static RestAppApiInterface getRestAppApiClient() {
if (sRestAppService == null) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
sRestAppService = restAdapter.create(RestAppApiInterface.class);
}
return sRestAppService;
}

}

我这样在 AsynTask 中调用服务

RestAppApiInterface apiInterface = ApiClient.getRestAppApiClient();
apiInterface.logIn("username","p@55w0rd");

最佳答案

[未测试,从我的头顶]

看起来你可以在这里进行干预:

Caused by ... yada yada ...
retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)

Retrofit 的 RestAdapter.Builder 允许您使用 setClient() 方法自定义访问后端的方式,因此您可以像这样:

RestAdapter restAdapter = new RestAdapter.Builder().setClient(
new Client.Provider() {
public Client get() {
return new UrlConnectionClient() {
@Override
public Response execute(Request request) {
// Test avail. / handle exceptions here,
// call through to the real implementation
return super.execute();
}
};
}
});

对于实际的主机可用性逻辑,您可以尝试 this recipe

关于android - 改造 |句柄 "server not found exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com