Android Volley “Unknown Host Exception” when tried to fetch IPV6 address

微笑、不失礼 提交于 2019-12-25 04:12:31

问题


Im trying to get IPV6 public address via volley.

So far, i managed to toast ip address for IPV4 but when volley tries to fetch IPV6 data, it throws Unknown Host Exception even the code for both method were same except the url part..

What happen? I already give internet permission in manifest.

My implementation were as below:

Service file

 @Override
    public int onStartCommand(Intent pIntent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Restart service!", Toast.LENGTH_LONG).show();

        PublicIPResolver.getIPV4(GooglePlayService.this, new Response.Listener<String>() {
            @Override
           public void onResponse(String s) {
               Toast.makeText(GooglePlayService.this,s,Toast.LENGTH_SHORT).show();
           }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError volleyError) {
                   Toast.makeText(GooglePlayService.this,volleyError.getMessage(),Toast.LENGTH_SHORT).show();
               }
           });

        PublicIPResolver.getIPV6(GooglePlayService.this, new Response.Listener<String>() {
            @Override
            public void onResponse(String r) {
                Toast.makeText(GooglePlayService.this,r,Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(GooglePlayService.this,volleyError.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });


        return super.onStartCommand(pIntent, flags, startId);
    }

PublicIPResolver class

public class PublicIPResolver {

    public static void getIPV4(Context context, Response.Listener<String> ip, Response.ErrorListener errorListener)
    {
        RequestQueue queue = Volley.newRequestQueue(context);
        String url ="http://v4.ipv6-test.com/api/myip.php";
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, ip,errorListener);
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    public static void getIPV6(Context context, Response.Listener<String> ip, Response.ErrorListener errorListener)
    {
        RequestQueue queue = Volley.newRequestQueue(context);
        String url ="http://v6.ipv6-test.com/api/myip.php";
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, ip,errorListener);
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }
}

来源:https://stackoverflow.com/questions/41025825/android-volley-unknown-host-exception-when-tried-to-fetch-ipv6-address

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