com.android.volley.NoConnectionError: java.net.UnknownHostException

做~自己de王妃 提交于 2019-12-25 14:11:54

问题


i have to do some network operations with Volly. i am getting com.android.volley.NoConnectionError: java.net.UnknownHostException for below Code.

String url="https://www.user.url.in"


        JsonObjectRequest medStats = new JsonObjectRequest(Request.Method.PUT, url, object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            Boolean success = response.getBoolean("success");
                            System.out.println("On Success" + success);

                            // Toast.makeText(getApplicationContext(),""+success,Toast.LENGTH_SHORT).show();
                            //String message=response.getString("message");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(context, "Error from server : " + error.toString(), Toast.LENGTH_LONG).show();

                Toast.makeText(context,Boolean.toString(isNetworkAvailable()),Toast.LENGTH_LONG).show();

                String message = null;
                if (error instanceof NetworkError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                } else if (error instanceof ServerError) {
                    message = "The server could not be found. Please try again after some time!!";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                } else if (error instanceof AuthFailureError) {
                    message = "Authentication Error!!";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                } else if (error instanceof ParseError) {
                    message = "Parsing error! Please try again after some time!!";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                } else if (error instanceof NoConnectionError) {
                    message = "Communication Error!!";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                } else if (error instanceof TimeoutError) {
                    message = "Connection TimeOut! Please check your internet connection.";
                    Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                }

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                headers.put("x-access-token", token);
                return headers;
            }
        };

        requestQueue.add(medStats);

My Attempts:

Check Manifest file:- 1.

are added

  1. to checked url ,using Postman
  2. Internet is also working

Logcat Details:

10-01 23:20:33.913 18856-18856/user.com.test2 W/System.err: com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "www.user.url.in": No address associated with hostname
10-01 23:20:33.913 18856-18856/user.com.test2 W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
10-01 23:20:33.913 18856-18856/user.com.test2 W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
10-01 23:20:33.914 18856-18856/user.com.test2 W/System.err: Caused by: java.net.UnknownHostException: Unable to resolve host "www.user.url.in": No address associated with hostname
10-01 23:20:33.914 18856-18856/user.com.test2 W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:470)
10-01 23:20:33.914 18856-18856/user.com.test2 W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-01 23:20:33.915 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-01 23:20:33.916 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
10-01 23:20:33.916 18856-18856/user.com.test2 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
10-01 23:20:33.916 18856-18856/user.com.test2 W/System.err:     at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:264)
10-01 23:20:33.916 18856-18856/user.com.test2 W/System.err:     at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
10-01 23:20:33.916 18856-18856/user.com.test2 W/System.err:     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
10-01 23:20:33.919 18856-18856/user.com.test2 W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
10-01 23:20:33.919 18856-18856/user.com.test2 W/System.err:     ... 1 more
10-01 23:20:33.919 18856-18856/user.com.test2 W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-01 23:20:33.919 18856-18856/user.com.test2 W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
10-01 23:20:33.920 18856-18856/user.com.test2 W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
10-01 23:20:33.920 18856-18856/user.com.test2 W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
10-01 23:20:33.920 18856-18856/user.com.test2 W/System.err:     ... 18 more

回答1:


I got the solution. When dealing with the sub domain while using volley. Don't use www. So the correct format is String url="http://user.url.in




回答2:


If an Android Volley post request fails due to network loss, will Android Volley retry the post after the network connection is restored automatically? No, it won't. I might not even be desired depending on your application.

Will it fire all of the request attempts, wait for connection to reestablish or simply trigger an error and stop? It simply throws an error. And yes, you should write this kind of logic yourself.




回答3:


setRetryPolicy() for volley also increase the timeOut and Retry counts. Hope it helps.



来源:https://stackoverflow.com/questions/39810487/com-android-volley-noconnectionerror-java-net-unknownhostexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!