java.net.UnknownHostException:无法解析主机“414.shtml”:没有与主机名关联的地址用于Retrofit api hitting

时间:2017-12-28 13:23:04

标签: android retrofit2

我正在使用retrofit2进行api命令,参数是String类型。

当字符串值很小时,它工作正常,但是当字符串数据很大,然后得到以下异常。

java.net.UnknownHostException: Unable to resolve host "414.shtml": No address associated with hostname
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:125)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
    at java.net.InetAddress.getAllByName(InetAddress.java:752)
    at okhttp3.Dns$1.lookup(Dns.java:39)
    at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
    at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
    at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
    at okhttp3.RealCall.getResponse(RealCall.java:243)
    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
    at okhttp3.RealCall.access$100(RealCall.java:30)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    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:762)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    at libcore.io.Posix.android_getaddrinfo(Native Method)
    at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:106)
enter code here

1 个答案:

答案 0 :(得分:0)

我在参数中发送了json,因此我得到了java.net.UnknownHostException: Unable to resolve host "414.shtml"

为此,我们必须在请求正文中发送json。 以下代码使用请求正文发送json。

RequestBody body = null;
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data", data);

            body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

public interface ApiInterface {
    @POST
    Call<DemoResponse> apitest(@Url String url, @Body RequestBody body);
}