Android Volley" Unknown Host Exception"当试图获取IPV6地址时

时间:2016-12-07 19:33:03

标签: android android-volley ipv6

我试图通过凌空获得IPV6公共地址。

到目前为止,我设法为IPV4提供ip地址但是当volley尝试获取IPV6数据时,它会抛出未知主机异常,即使两个方法的代码都相同,除了url部分.. < / p>

会发生什么?我已经在清单中提供了互联网许可。

我的实施如下:

服务文件

 @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类

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);
    }
}

0 个答案:

没有答案