问题
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