HttpURLConnection getResponseCode()不起作用

时间:2016-02-15 12:48:28

标签: java android url

我遇到问题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

请帮助我!

感谢。

1 个答案:

答案 0 :(得分:0)

对我来说,看起来代码是正确的。至少这可以按预期工作:

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

}

您确定您的网址有效吗?如果是这样,你试过调试吗?