0

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)
TadeoArmenta
  • 154
  • 6
  • 16

1 Answers1

1

I don't think that exception is being thrown when you call your exists method. If an exception was being thrown there, it would be caught by this clause:

    catch (IOException ex){
        return 99;
    }

In fact, I think the exception is coming from somewhere else in your application ... where you are attempting to interact with GAE. (My guess is that you've gotten the GAE configuration wrong somehow.)

(Incidentally, that handler is a bad idea. If that exception did occur, you are throwing away any information about the cause. You should at least log the exception, using the standard Android logging API.)


Now that I can see a proper stacktrace, the root cause of your problems is leaps out at me ...

The actual root exception is this: android.os.NetworkOnMainThreadException. That exception is thrown when you attempt to perform a network request on your app's main event thread. That is not allowed because would cause your app's user interface to lock up. You need to use AsyncTask or similar to perform the network request.

Read this Q&A for a more detailed explanation: How to fix android.os.NetworkOnMainThreadException?


The rest of the stacktrace gives the methods and and line numbers for the calls that were in progress on the current thread when the exception was thrown. If you have access to your Android platform's source code you can s long way towards figure out what the code was doing and how it got there. (It is not necessary here ... because the thrown exception is enough to explain what has gone wrong.)

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for the advice about logging api, i'm new in android XD.The exception is catched by the expression, but you are right, because it doesn't keeps why, i had to use debugger functionality of android studio to get the exception cause. – TadeoArmenta Feb 02 '14 at 05:34
  • I'm contending (in part) that the exception you are actually getting is NOT being thrown when you attempt to get the headers. If you log the exception properly, the log event (or lack of one!) will either confirm or disprove that contention. – Stephen C Feb 02 '14 at 05:37
  • sorry, about my english! i try to say that I actually use the debugger to ensure where the exception iS made, and unfortunately, it is made on the execution or the "exist" method. – TadeoArmenta Feb 02 '14 at 05:52
  • So what does the stacktrace say? Add it to the Question. – Stephen C Feb 02 '14 at 06:26
  • I just added stack trace, but i don't know what it means :( – TadeoArmenta Feb 02 '14 at 15:18
  • That's not a stacktrace format that I recognize. Call `Throwable.printStackTrace()` on the exception object and show us what that says. – Stephen C Feb 02 '14 at 15:23
  • i added Throwable.printStackTrace(), i don't know if I'm doing it well. Where I can find the stack trace output? Logcat? because i can't see nothing relevant :/ Or just I don't know what to looking for :'( – TadeoArmenta Feb 02 '14 at 15:57