![](/img/trans.png)
[英]HttpURLConnection throws NullPointerException at getResponseCode
[英]HttpURLConnection getResponseCode() not working
我的getResponseCode()
有問題,它不起作用:
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.IOException;
功能:
public static int checkURL(String u) throws IOException {
java.net.URL url = new URL(u);
System.out.println("LLEGA :::::::::: " + url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int code = connection.getResponseCode();
System.out.println("HHHHHHHHHHHHHHHHHHHHHHH "+code);
return code;
}
logcat的:
System.out:LLEGA:::::::::: http://XXX.XXX.XXX.XXX/webservice_balanza/deboinventario/webservice.php
System.out: (HTTPLog)-Static: isSBSettingEnabled false
System.out: (HTTPLog)-Static: isShipBuild true
System.out: (HTTPLog)-Thread-1-249171107: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
System.out: (HTTPLog)-Thread-1-249171107: SMARTBONDING_FEATURE_ENABLED is true
System.out: (HTTPLog)-Static: isSBSettingEnabled false
請幫我 !
謝謝。
在我看來,代碼是正確的。 至少這可以按預期工作:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
System.out.println(code);
}
}
您確定您的URL有效嗎? 如果是這樣,您是否嘗試過調試?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.