doulu1907
doulu1907
采纳率100%
2014-02-05 19:53

在从mysql数据库-Android中提取数据之前,请确保wamp服务器处于联机状态

已采纳

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:

<?php

//receive datatbase Info. from android app
$json=$_SERVER['HTTP_JSON'];
var_dump($data);
$data=json_decode($json);
$dbname=$data->dbname;
$dbpass=$data->dbpass;
$link=mysql_connect("localhost","root",$dbpass);

if (!$link) {
    echo "error"; 
    die(mysql_error());
}

$db_selected=mysql_select_db($dbname,$link);

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

mysql_query("set names 'utf8'");
mysql_query("set CHARACTER set 'utf8'",$link);
mb_internal_encoding("UTF-8");

?>

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!

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

3条回答

  • 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(){
            @Override
            public void run() {
                try {
                    try {
                        URL url = new URL("http://220.13.81.33");
    
                        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                        urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds
                        urlc.connect();
    
                        if (urlc.getResponseCode() == 301) {
    
    
                        }
                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    catch (Exception e)
                    {
    
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    
        thread.start(); 
    

    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();
    StrictMode.setThreadPolicy(policy); 
    

    2-If you are working on emulator and you want to connect on the local host, you have to use this address http://10.0.2.2:8080 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 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes , 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?

    <?php
        $link = @mysql_connect('127.0.0.1','a_userid','a_password');
        $err = mysql_errno();
        switch ($err) {
            case 2002 :
                echo 'Error Start MySQL Please';
                exit;
            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!");
    } 
    
    点赞 评论 复制链接分享