【问题标题】:when insert new data in MySQL display notification in android当在 MySQL 中插入新数据时在 android 中显示通知
【发布时间】:2018-05-23 18:42:53
【问题描述】:

我尝试 在通知中从 Mysql db 开发了一个简单的 android 应用程序视图消息 我的问题是,如果我想每 2 分钟从数据库获取信息,或者检查是否有任何新数据已发布到数据库,我该如何在 android 中完成? 我用这段代码托盘了几次,但结果总是没有舔图像 Frem explorer from android 注意:数据库正在运行 sev_data 类:

  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));
?>

【问题讨论】:

  • 在你的代码中加入几个Log.e(),看看哪里会出现问题。

标签: php android mysql push-notification


【解决方案1】:

我认为您的查询永远不会执行,因为您的网址格式错误(您放置 > 而不是 =),所以 $_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;

【讨论】:

  • 现在应用程序在替换它后显示所有(id,用户名,图像,电子邮件)行 id1,id2,id3 等我只想显示数据库中最后一个 ID 中的名称谢谢你关于你的帮助
  • 因此您必须将查询更改为SELECT name FROM mha1 where id&gt;,然后将此值放入您的json数组或对象中
  • 您能否编辑我的代码以显示来自最后一个 ID 而不是所有 ID 的名称
  • php 结果是什么?
  • php 结果为“日期”
猜你喜欢
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2018-05-14
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多