gpt4 book ai didi

java - 在android中获取http header ?

转载 作者:行者123 更新时间:2023-12-01 13:32:12 25 4
gpt4 key购买 nike

我正在尝试使用我在此处找到的代码从众所周知的网址获取http header ,但无法获取任何 header ,我总是会出现异常。

我的代码是:

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

}

}

异常(exception)情况是:

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

据我了解,异常(exception)情况是我不提供服务器或主机,或者只是我提供了一个未知的服务器或主机,即使我将 google.com 设置为 url

这是执行exist方法时的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)

最佳答案

我认为当您调用 exists 方法时不会引发异常。如果在那里抛出异常,它将被这个子句捕获:

    catch (IOException ex){
return 99;
}

事实上,我认为异常来自应用程序中的其他地方......您尝试与 GAE 交互的地方。 (我的猜测是您的 GAE 配置不知何故错误。)

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

<小时/>

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

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

阅读此问答以获得更详细的解释:How to fix android.os.NetworkOnMainThreadException?

<小时/>

堆栈跟踪的其余部分提供抛出异常时当前线程上正在进行的调用的方法和行号。如果您可以访问 Android 平台的源代码,您可以很容易地弄清楚代码的用途以及它是如何到达那里的。 (这里没有必要……因为抛出的异常足以解释出了什么问题。)

关于java - 在android中获取http header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21507119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com