I'm trying to get http headers from well known url, using code i'd found here, but can't get any, i always rise an exception.
My code is:
public int exist(String urlString){
try {
URL u = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setInstanceFollowRedirects(false);
huc.setRequestMethod("HEAD");
huc.connect();
return huc.getResponseCode();
}
catch (IOException ex){
Throwable t = new Throwable();
t.printStackTrace();
return 99;
}
}
public void asct(View v) {
TextView estNumTextBox;
estNumTextBox = (EditText) findViewById(R.id.txtboxEstnum);
String estNumVal;
estNumVal = estNumTextBox.getText().toString();
String inURL;
// inURL = "http://es" + estNumVal + ".no-ip.org/emax/";
inURL = "http://www.google.com";
String responseCode;
responseCode = String.valueOf(exist(inURL));
TextView salidaTextBox;
salidaTextBox = (TextView) findViewById(R.id.salida);
if (responseCode.equals("200")){
salidaTextBox.append("PAGINA... 00k" + "\n" + responseCode + "\n");
}
else {
salidaTextBox.append("PAGINA... NoT00k" + "\n" + responseCode + "\n");
}
}
the exception is:
this = {libcore.io.BlockGuardOs@830018887648}
Exception = {libcore.io.GaiException@830028334696}
functionName = {java.lang.String@830028334616}"getaddrinfo"
error = 8
cause = {libcore.io.GaiException@830028334696}"libcore.io.GaiException: getaddrinfo failed: EAI_NONAME (hostname nor servname provided, or not known)"
detailMessage = null
stackState = {int[62]@830028334736}
[0] = 1458753280
[1] = 0
[2] = 1458835536
[3] = 2
[4] = 1458689528
[5] = 48
[6] = 1458688576
[7] = 17
[8] = 1458688520
[9] = 0
[10] = 1461955488
[11] = 13
[12] = 1461955544
[13] = 0
[14] = 1461957384
[15] = 3
[16] = 1461956608
[17] = 57
[18] = 1461955600
[19] = 90
[20] = 1461950184
[21] = 22
[22] = 1461949120
[23] = 4
[24] = 1461948832
[25] = 4
[26] = 1461950464
[27] = 95
[28] = 1461944176
[29] = 5
[30] = 1461868208
[31] = 20
[32] = 1461868152
[33] = 42
[34] = 1458630208
[35] = 0
[36] = 1458631336
[37] = 17
[38] = 1461929848
[39] = 46
[40] = 1459080384
[41] = 18
[42] = 1460507992
[43] = 2
[44] = 1459253896
[45] = 2
[46] = 1459253960
[47] = 4
[48] = 1458969136
[49] = 84
[50] = 1459393704
[51] = 87
[52] = 1458630208
[53] = 0
[54] = 1458631336
[55] = 17
[56] = 1461191704
[57] = 11
[58] = 1459241008
[59] = 66
[60] = 1458650080
[61] = 0
stackTrace = null
suppressedExceptions = {java.util.Collections$EmptyList@830018863080} size = 0
the exception,as far as I reach to understand it, says that I does not provide server or host, or it simply I provide it an unknown server or host, even when I set google.com as url
here is the logcat output at the time of execution of exist method:
W/System.err﹕ android.os.NetworkOnMainThreadException
02-02 09:05:21.659 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1120)
02-02 09:05:21.660 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:419)
02-02 09:05:21.660 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:270)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:245)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-02 09:05:21.661 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-02 09:05:21.662 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
02-02 09:05:21.662 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
02-02 09:05:21.663 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-02 09:05:21.663 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-02 09:05:21.664 12504-12504/com.dat30.smaxpwner W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
02-02 09:05:21.664 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.dat30.smaxpwner.MainActivity.exist(MainActivity.java:216)
02-02 09:05:21.665 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.dat30.smaxpwner.MainActivity.asct(MainActivity.java:235)
02-02 09:05:21.665 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-02 09:05:21.666 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-02 09:05:21.666 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View$1.onClick(View.java:3603)
02-02 09:05:21.668 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View.performClick(View.java:4101)
02-02 09:05:21.668 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View$PerformClick.run(View.java:17088)
02-02 09:05:21.668 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
02-02 09:05:21.669 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 09:05:21.670 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Looper.loop(Looper.java:153)
02-02 09:05:21.670 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5093)
02-02 09:05:21.670 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-02 09:05:21.670 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-02 09:05:21.671 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-02 09:05:21.671 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-02 09:05:21.672 12504-12504/com.dat30.smaxpwner W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
02-02 09:05:21.672 12504-12504/com.dat30.smaxpwner I/System.out﹕ [CDS] fix other exception in HttpUrlConnection
02-02 09:05:21.673 12504-12504/com.dat30.smaxpwner W/System.err﹕ java.lang.Throwable
02-02 09:05:21.675 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.dat30.smaxpwner.MainActivity.exist(MainActivity.java:220)
02-02 09:05:21.675 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.dat30.smaxpwner.MainActivity.asct(MainActivity.java:235)
02-02 09:05:21.675 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-02 09:05:21.676 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-02 09:05:21.676 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View$1.onClick(View.java:3603)
02-02 09:05:21.676 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View.performClick(View.java:4101)
02-02 09:05:21.676 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.view.View$PerformClick.run(View.java:17088)
02-02 09:05:21.676 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.os.Looper.loop(Looper.java:153)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5093)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-02 09:05:21.677 12504-12504/com.dat30.smaxpwner W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)