android ping一个URL(不打开)

时间:2014-04-11 05:55:26

标签: android url ping hit

大家好,我需要做的一件事就是...... 点击我做过的任何网址

public class UrlPinger extends AsyncTask<URL, Void, Void> {

        @Override
        protected Void doInBackground(URL... urls) {
            URL url = urls[0];

            try {
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.connect();
                System.out.println("thisi si wie URL>>>"+url+"");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("ERROR "+e.getMessage()+"");
            }

            return null;

        }

上面的类没有给我任何错误但是也没有打到URL我错了什么? 并像这样称呼它

UrlPinger pinger = new UrlPinger();
                    pinger.execute(myURL);// this is perfect url copy pase in browser hits

给了<uses-permission android:name="android.permission.INTERNET"></uses-permission> 我的LOGCAT:

04-11 11:32:09.206: D/StatusBar.NetworkController(1154): refreshViews: Data not connected!! Set no data type icon
04-11 11:32:09.206: D/StatusBar.NetworkController(1154): refreshViews connected={ wifi } level=4 combinedSignalIconId=0x7f0200c1/com.android.systemui:drawable/stat_sys_wifi_signal_4_fully combinedActivityIconId=0x7f0200b1 mobileLabel=BSNL MOBILE wifiLabel=LeewayxxxxXXXXxxxxXXXX emergencyOnly=false combinedLabel=LeewayxxxxXXXXxxxxXXXX mAirplaneMode=false mDataActivity=0 mPhoneSignalIconId=0x7f0200a7 mDataDirectionIconId=0x0 mDataSignalIconId=0x7f0200a7 mDataTypeIconId=0x0 mNoSimIconId=0x0 mWifiIconId=0x7f0200c1 mBluetoothTetherIconId=0x108076a
04-11 11:32:09.206: D/StatusBar.NetworkController(1154): changing data overlay icon id to 2130837681
04-11 11:32:10.657: D/StateMachine(651): handleMessage: E msg.what=131155
04-11 11:32:10.657: D/StateMachine(651): processMsg: ConnectedState
04-11 11:32:10.657: D/WifiStateMachine(651): ConnectedState{ what=131155 when=-6ms arg1=1121 }
04-11 11:32:10.657: D/StateMachine(651): processMsg: L2ConnectedState
04-11 11:32:10.657: D/WifiStateMachine(651): L2ConnectedState{ what=131155 when=-9ms arg1=1121 }
04-11 11:32:10.657: I/wpa_supplicant(17672): wlan0: Control interface command 'SIGNAL_POLL'
04-11 11:32:10.667: D/StateMachine(651): handleMessage: X
04-11 11:32:11.208: W/SignalStrength(1154): getLTELevel - rsrp:2147483647 snr:2147483647 rsrpIconLevel:-1 snrIconLevel:-1
04-11 11:32:11.208: W/SignalStrength(1154): getLTELevel - rssi:99 rssiIconLevel:0
04-11 11:32:11.208: W/SignalStrength(1154): getTdScdmaLevel = 0
04-11 11:32:11.208: W/SignalStrength(1154): getGsmLevel=4
04-11 11:32:11.208: W/SignalStrength(1154): getLevel=4
04-11 11:32:11.208: D/StatusBar.NetworkController(1154): refreshViews: Data not connected!! Set no data type icon
04-11 11:32:11.208: D/StatusBar.NetworkController(1154): refreshViews connected={ wifi } level=4 combinedSignalIconId=0x7f0200c1/com.android.systemui:drawable/stat_sys_wifi_signal_4_fully combinedActivityIconId=0x0 mobileLabel=BSNL MOBILE wifiLabel=LeewayxxxxXXXXxxxxXXXX emergencyOnly=false combinedLabel=LeewayxxxxXXXXxxxxXXXX mAirplaneMode=false mDataActivity=0 mPhoneSignalIconId=0x7f0200a7 mDataDirectionIconId=0x0 mDataSignalIconId=0x7f0200a7 mDataTypeIconId=0x0 mNoSimIconId=0x0 mWifiIconId=0x7f0200c1 mBluetoothTetherIconId=0x108076a
04-11 11:32:11.218: D/StatusBar.NetworkController(1154): changing data overlay icon id to 0
04-11 11:32:13.681: D/StateMachine(651): handleMessage: E msg.what=131155
04-11 11:32:13.681: D/StateMachine(651): processMsg: ConnectedState
04-11 11:32:13.681: D/WifiStateMachine(651): ConnectedState{ what=131155 when=-3ms arg1=1121 }
04-11 11:32:13.681: D/StateMachine(651): processMsg: L2ConnectedState
04-11 11:32:13.681: D/WifiStateMachine(651): L2ConnectedState{ what=131155 when=-6ms arg1=1121 }
04-11 11:32:13.681: I/wpa_supplicant(17672): wlan0: Control interface command 'SIGNAL_POLL'
04-11 11:32:13.691: D/StateMachine(651): handleMessage: X
04-11 11:32:16.694: D/StateMachine(651): handleMessage: E msg.what=131155
04-11 11:32:16.694: D/StateMachine(651): processMsg: ConnectedState
04-11 11:32:16.694: D/WifiStateMachine(651): ConnectedState{ what=131155 when=-5ms arg1=1121 }
04-11 11:32:16.694: D/StateMachine(651): processMsg: L2ConnectedState
04-11 11:32:16.694: D/WifiStateMachine(651): L2ConnectedState{ what=131155 when=-7ms arg1=1121 }
04-11 11:32:16.694: I/wpa_supplicant(17672): wlan0: Control interface command 'SIGNAL_POLL'
04-11 11:32:16.714: D/StateMachine(651): handleMessage: X
04-11 11:32:19.707: D/StateMachine(651): handleMessage: E msg.what=131155
04-11 11:32:19.707: D/StateMachine(651): processMsg: ConnectedState
04-11 11:32:19.707: D/WifiStateMachine(651): ConnectedState{ what=131155 when=-4ms arg1=1121 }
04-11 11:32:19.717: D/StateMachine(651): processMsg: L2ConnectedState
04-11 11:32:19.717: D/WifiStateMachine(651): L2ConnectedState{ what=131155 when=-6ms arg1=1121 }
04-11 11:32:19.717: I/wpa_supplicant(17672): wlan0: Control interface command 'SIGNAL_POLL'
04-11 11:32:19.727: D/StateMachine(651): handleMessage: X
04-11 11:32:22.730: D/StateMachine(651): handleMessage: E msg.what=131155
04-11 11:32:22.730: D/StateMachine(651): processMsg: ConnectedState
04-11 11:32:22.730: D/WifiStateMachine(651): ConnectedState{ what=131155 when=-5ms arg1=1121 }
04-11 11:32:22.730: D/StateMachine(651): processMsg: L2ConnectedState
04-11 11:32:22.740: D/WifiStateMachine(651): L2ConnectedState{ what=131155 when=-6ms arg1=1121 }
04-11 11:32:22.740: I/wpa_supplicant(17672): wlan0: Control interface command 'SIGNAL_POLL'
04-11 11:32:22.750: D/StateMachine(651): handleMessage: X

1 个答案:

答案 0 :(得分:0)

HttpURLConnection connection = null;
    try {
        URL u = new URL("http://www.google.com/");
        connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("HEAD");
        int code = connection.getResponseCode();
        System.out.println("" + code);
        // You can determine on HTTP return code received. 200 is success.
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }