Question

Hello all in one of my application i need to do is... hit any URL i have done this

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;

        }

the above class not giving me any error but not also hit the URL what wrong i am going? and calling it like this

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

have given <uses-permission android:name="android.permission.INTERNET"></uses-permission> My 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
Was it helpful?

Solution

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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top