当数据mysql db发生变化时,在mainActivity上显示数据

时间:2017-12-13 14:23:21

标签: android json

此应用程序的主要目的是登录并显示来自mysql的存储数据并显示它。好吧,我已经完成了那个非常有效的方法。我现在需要的是应用程序在mainActivity上自动显示sql表中任何更新或更改的结果。

包com.dedykuncoro.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button btn_logout;
    TextView txt_id, txt_username,txt_t_data;
    String id, username, t_data;
    SharedPreferences sharedpreferences;

    public static final String TAG_ID = "id";
    public static final String TAG_USERNAME = "username";
    public static final String TAG_t_data = "t_data";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_id = (TextView) findViewById(R.id.txt_id);
        txt_username = (TextView) findViewById(R.id.txt_username);
        txt_t_data = (TextView) findViewById(R.id.txt_t_data);
        btn_logout = (Button) findViewById(R.id.btn_logout);

        sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);

        id = getIntent().getStringExtra(TAG_ID);
        username = getIntent().getStringExtra(TAG_USERNAME);
        t_data = getIntent().getStringExtra(TAG_t_data);

        txt_id.setText("ID : " + id);
        txt_username.setText("USERNAME : " + username);
        txt_t_data.setText("Data Used : " + t_data);

        btn_logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // update login session ke FALSE dan mengosongkan nilai id dan username
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putBoolean(Login.session_status, false);
                editor.putString(TAG_ID, null);
                editor.putString(TAG_USERNAME, null);
                editor.putString(TAG_t_data, null);
                editor.commit();

                Intent intent = new Intent(MainActivity.this, Login.class);
                finish();
                startActivity(intent);
            }
        });


    }
}

从MYSQL中获取数据的代码

class usr {}

$username = $_POST["username"];
$password = $_POST["password"];

if ((empty($username)) || (empty($password))) { 
    $response = new usr();
    $response->success = 0;
    $response->message = "This Column Cannot Be Empty"; 
    die(json_encode($response));
}

$query = mysql_query("SELECT * FROM radcheck WHERE username='$username' AND value='$password'");

$row = mysql_fetch_array($query);

if (!empty($row)){
    $response = new usr();
    $response->success = 1;
    $response->message = "Welcome ".$row['username'];
    $response->id = $row['id'];
    $response->username = $row['username'];
$query_dused="SELECT radusergroup.username,sum((radacct.acctinputoctets+radacct.acctoutputoctets)) AS T_Data, SUM(TIMESTAMPDIFF(second,radacct.acctstarttime, radacct.acctstoptime)) AS timeUsed, radusergroup.groupname AS Profile From radacct LEFT JOIN radusergroup ON radusergroup.username=radacct.username WHERE radusergroup.username='$username'"; $d_used = mysql_query($query_dused);while ($line = mysql_fetch_array($d_used, MYSQL_ASSOC)) {$response->t_data = round(($line['T_Data']/1000000000), 2);$response->t_profile= $line['Profile'];$response->t_time= date("H:i:s",$line['timeUsed']);}die(json_encode($response));} else {$response = new usr(); $response->success = 0;$response->message = "Invalid Username or Password";die(json_encode($response));}mysql_close();?>

0 个答案:

没有答案