想要达到这样的目的:如果服务器关闭了,则客户端要提示“连接服务器失败”。
连接服务器遇到问题,如果服务器开着,使用if(conn.getResponseCode() == 200)就能得到响应码,但是,一旦服务器关闭,遇到getResponseCode()方法整个程序就卡在这了,很是郁闷,请问怎么解决啊???
使用测试打印出来,判断是遇到getResponseCode()方法程序就不能运行了

解决方案 »

  1.   

    如果服务器关闭了,在你建立连接时就会有IOException抛出。像这个方法:public URLConnection openConnection ()
    Since: API Level 1Opens a connection to the remote resource specified by this URL. This connection allows bidirectional data transfer.
    Returns    the connection to this URL.Throws
        IOException  if an error occurs while opening the connection.