2014-02-05 19:53



Before to get data from mysql database I need to make sure that the wamp server is online, if not, the app tell the user to put it online before pull data, how can this achieve in android? as if it is offline android app stopped!

PHP code to conect to database is:


//receive datatbase Info. from android app

if (!$link) {
    echo "error"; 


if (!$db_selected) {
    die ('error'. mysql_error());

mysql_query("set names 'utf8'");
mysql_query("set CHARACTER set 'utf8'",$link);


But in andorid app it does not receive "error" when there is no connection (I mean when wamp server is offline)?

In sum please, If there is any way to make sure that the wamp server is online before pull data start from database? and if not give user msg.

with tired mood I tried but with no result, really any help will be appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dou4381 dou4381 7年前

    One of the possible solution for you question is by creating an Http request for the wamp server address and that can be easily done in Android. The following code shows you how the http request is created for any URL you need.

    Thread thread = new Thread(new Runnable(){
            public void run() {
                try {
                    try {
                        URL url = new URL("");
                        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                        urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds
                        if (urlc.getResponseCode() == 301) {
                    } catch (MalformedURLException e1) {
                    } catch (IOException e) {
                    catch (Exception e)
                } catch (Exception e) {

    It is obvious that you just put the IP or URL that you want in the URL constructor, but you have to take care about two important notes:

    1-To make a request, you need to do that by using threading or by creating async task or, if you don't want to use threading you can enforce the android to deal with such requests in the main process of the android by adding the following policy in the code. However, this kind of solution needs SDK version more than 9.

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    2-If you are working on emulator and you want to connect on the local host, you have to use this address since it is the local host address for Android.

    Once the connect function is issued, a respond code will be returned otherwise an exception will be arise. The interpretations of the respond codes can be found in this link , note please if the returned respond code was 301 that means the used will be redirected to the given URI and this an indication the Wamp sever is online. For the arise exceptions, most likely the Wamp server is offline. Thus, you need to do several testing for different scenario and see the outputs.

    I hope that everything goes very well with you.

    点赞 评论 复制链接分享
  • dsfds2343 dsfds2343 7年前

    Would this not be a lot simpler?

        $link = @mysql_connect('','a_userid','a_password');
        $err = mysql_errno();
        switch ($err) {
            case 2002 :
                echo 'Error Start MySQL Please';
            case xxxx : // any other possible errors    
        // MySQL is there so proceed as normal
    点赞 评论 复制链接分享
  • duanlan8763 duanlan8763 7年前

    I answered that within another question, but it might be an answer to this one too. My solution, basically, is based on setting a Socket to your wamp server 80 port.

    I use the following code on many of my projects:

    Socket socket;
    // Adjust it to your case, it might be a public IP too
    final String host = "192.168.1.X";
    final int port = 80;
    final int timeout = 30000;   // 30 seconds of timeout
    try {
      socket = new Socket();
      socket.connect(new InetSocketAddress(host, port), timeout);
    catch (UnknownHostException uhe) {
      Log.e("WampSock", "I couldn't resolve the host you've provided!");
    catch (SocketTimeoutException ste) {
      Log.e("WampSock", "After a reasonable amount of time, I'm not able to connect, your WAMP server is probably down!");
    catch (IOException ioe) {
      Log.e("WampSock", "Hmmm... Sudden disconnection, probably you should retry once again!");
    点赞 评论 复制链接分享