在android中在mysql中插入新数据时显示通知

qhhrdooz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(385)

我试着从mysql数据库开发一个简单的android应用程序视图消息,我的问题是,如果我想每隔2分钟从数据库获取信息,或者检查是否有新的数据已经发布到数据库,我怎么能在android中做到这一点?我用这段代码托盘了好几次,但结果总是来自android的nothing lick image frem explorer注意:数据库正在运行sev\u数据类:

public class sev_data extends IntentService {
public static boolean ServiceIsRun = false;
public sev_data() {
    super("MyWebRequestService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
    while (ServiceIsRun) {
        int id_s=0;
        String url0 = ""http://172.17.100.2/sendapp.php?co>"+id_s;
        String  msg;
        try {
            URL url = new URL(url0);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            msg = Stream2String(in);
            in.close();
            JSONObject jsonRootObject = new JSONObject(msg);
            JSONArray jsonArray = jsonRootObject.optJSONArray("date");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id= jsonObject.optString("id");
                String title = jsonObject.optString("title");
                String mess = jsonObject.optString("mess");
                 }
            // creat new intent
            intent = new Intent();
            //set the action that will receive our broadcast
            intent.setAction("com.example.Broadcast");
            // add data to the bundle
            intent.putExtra("msg", msg);
            // send the data to broadcast
            sendBroadcast(intent);
            //delay for 50000ms
        } catch (Exception e) {
        }
        try{
            Thread.sleep(20000);
        }catch (Exception ex){}
    }
}
String date="";
public String Stream2String(InputStream inputStream) {
    BufferedReader bureader=new BufferedReader( new InputStreamReader(inputStream));
    String line ;
    String Text="";
    try{
        while((line=bureader.readLine())!=null) {
            Text+=line;
        }
        inputStream.close();
    }catch (Exception ex){}
    return Text;
}

}
sendapp.php地址:

<?php
$hostname_mus_db = "localhost";
$database_mus_db = "musdb2";
$username_mus_db = "root";
$password_mus_db = "";

$tf=@mysql_connect($hostname_mus_db,$username_mus_db,$password_mus_db);
if(!$tf)
die('Connection problem1 ..');
$tf_db=mysql_select_db($database_mus_db);
if(!$tf_db)
{
	@mysql_close($tf_handle);
	die('selection problem2 ..');
}
$co_array=array();
if (isset($_GET['co'])) {
    $co=$_GET['co'];
	      $sql = mysql_query("SELECT * FROM mha1 where id>".$co);
	  while($row = mysql_fetch_assoc($sql)){
          $co_array[]=$row;
	  }
    }
header('Content-Type:application/json');
echo json_encode(array("date"=>$co_array));
?>
oyxsuwqo

oyxsuwqo1#

我认为您的查询从未执行过,因为您的url格式不正确(您将>而不是=),因此从未设置$\u get['co'],请尝试替换

String url0 = ""http://172.17.100.2/sendapp.php?co>"+id_s;

通过

String url0 = "http://172.17.100.2/sendapp.php?co="+id_s;

相关问题