gpt4 book ai didi

java - 未知主机异常 : name or service not known

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:55:02 25 4
gpt4 key购买 nike

我正在尝试使用 com.squareup.okhttp 中的 OkHttpClient 从 API 返回一些数据。我遇到了一些最终能够克服的错误,但我无法克服这个主机异常错误,而且这里的任何内容似乎都不足以解决我的案例。

下面是我尝试的代码及其输出,如果有人知道如何克服错误,我将不胜感激。

代码

public void connect() {         
OkHttpClient client = new OkHttpClient();

com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
.url("https://socialweb-analytics.lcloud.com/api/public/reports/jobs?companyKey=ato")
.get()
.addHeader("authorization", "Basic c2RidXNpbmVzc2FuYWx5dGljc0BhdG8uZ292LmF1OkFuYWx5dGljezEh")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "65ef5553-77b5-053f-9c01-4fdf76bdc92f")
.build();

System.out.println(request.toString());

try {
Response response = client.newCall(request).execute();
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}

输出

Request{method=GET, url=https://socialweb-analytics.lcloud.com/api/public/reports/jobs?companyKey=ato, tag=null}
java.net.UnknownHostException: socialweb-analytics.lcloud.com: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:922)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1314)
at java.net.InetAddress.getAllByName0(InetAddress.java:1267)
at java.net.InetAddress.getAllByName(InetAddress.java:1183)
at java.net.InetAddress.getAllByName(InetAddress.java:1119)
at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
at com.squareup.okhttp.Call.getResponse(Call.java:267)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
at com.squareup.okhttp.Call.execute(Call.java:79)
at technical_services.persistence.lithium.LithiumDataStore.connect1(LithiumDataStore.java:127)
at application.lithium_etl.LithiumTestController.main(LithiumTestController.java:24)

最佳答案

对于类似“java.net.UnknownHostException: [hostname]”的错误

原因是你的主机名不在/etc/hosts中,解决方法很简单:

sudo vim /etc/hosts

改变行看起来像:

127.0.0.1  localhost

到:

127.0.0.1  [hostname] localhost

保存并退出。如果问题仍然存在,您可能需要重新启动或运行:

sudo ifconfig eth0 down&&sudo ifconfig eth0 up

希望对您有所帮助!

关于java - 未知主机异常 : name or service not known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028842/

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