doulu1907 2014-02-05 19:53 采纳率: 100%
浏览 57
已采纳

在从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 2014-02-06 03:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条