在android中获取http标头?

时间:2014-02-02 04:40:01

标签: java android http exception http-headers

我正在尝试从知名网址获取http标头,使用我在此处找到的代码,但无法获取任何内容,我总是提出异常。

我的代码是:

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

        }

    }

例外是:

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

例外,据我了解它,说我不提供服务器或主机,或者只是我提供了一个未知的服务器或主机,即使我将google.com设置为url

这是执行存在方法时的logcat输出:

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)

1 个答案:

答案 0 :(得分:1)

我不认为在调用exists方法时会抛出异常。如果在那里抛出异常,它将被这个条款捕获:

    catch (IOException ex){
        return 99;
    }

事实上,我认为异常来自您应用程序中的其他位置......您尝试与GAE进行交互。 (我的猜测是你以某种方式弄错了GAE配置。)

(顺便说一下,这个处理程序是一个坏主意。如果确实发生了这种异常,你就丢弃了有关原因的任何信息。你应该至少使用标准的Android日志API来记录异常。)


现在我可以看到一个正确的堆栈跟踪,问题的根本原因是跳出给我...

实际的根异常是:android.os.NetworkOnMainThreadException。当您尝试在应用程序的主事件线程上执行网络请求时,会抛出该异常。这是不允许的,因为会导致应用的用户界面锁定。您需要使用AsyncTask或类似内容来执行网络请求。

阅读此Q&amp; A以获取更详细的说明:How to fix android.os.NetworkOnMainThreadException?


stacktrace的其余部分为抛出异常时当前线程上正在进行的调用提供方法和行号。如果您可以访问Android平台的源代码,那么您可以很长时间地弄清楚代码正在做什么以及它是如何实现的。 (这里没有必要......因为抛出的异常足以解释出错的原因。)