android Json自动刷新

时间:2014-09-25 18:05:28

标签: android json

我尝试让我的android类自动刷新,我正在从数据库中检索细节。它获取人物图片并通过数据库和其他一些细节在图像视图中显示...请问我如何进行自动刷新....... 这是我的PHP COD

<?php

//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {
    //gets user's info based off of a username.
    $query = " 
            SELECT 
                id, 
                username, 
                password,
                email,sex,name,age,status,pic,TIME
            FROM users
            WHERE 
                username = :username 
        ";

    $query_params = array(
        ':username' => $_POST['username']
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one to product JSON data:
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));

    }


    //This will be the variable to determine whether or not the user's information is correct.
    //we initialize it as false.
    $validated_info = false;

    //fetching all the rows from the query
    $row = $stmt->fetch();
    if ($row) {
        //if we encrypted the password, we would unencrypt it here, but in our case we just
        //compare the two passwords
        if ($_POST['password'] === $row['password']) {
            $login_ok = true;
        }
    }

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again s
    if ($login_ok) {
        $response["success"] = 1;
        $response["message"] = "Login successful!";

            $response["username"]=$row['username'];
            $response["email"]=$row['email'];
            $response["sex"]=$row['sex'];
            $response["name"]=$row['name'];
            $response["age"]=$row['age'];
            $response["status"]=$row['status'];
            $response["pic"]=$row['Pic'];
            $response["time"]=$row['Time'];
            $response["id"]=$row['id'];

        die(json_encode($response));
    } else {
        $response["success"] = 0;
        $response["message"] = "Invalid Credentials!";
        die(json_encode($response));
    }
} else {
?>
        <h1>Login</h1> 
        <form action="login.php" method="post"> 
            Username:<br /> 
            <input type="text" name="username" placeholder="username" /> 
            <br /><br /> 
            Password:<br /> 
            <input type="password" name="password" placeholder="password" value="" /> 
            <br /><br /> 
            <input type="submit" value="Login" /> 
        </form> 
        <a href="register.php">Register -                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               </a>
    <?php
}

?> 

MY ANDROID COD

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class CategoryActivity extends ListActivity {

    private static final String TAG_POSTS = "posts";
    public static final String TAG_ID = "id";
    public static final String TAG_NAME = "category";
    public static final String TAG_CATEGORIES_COUNT = "categories_count";
    public static final String TAG_CATEGORIES_LOGO = "categories_logo";

    private static final String URL_CATEGORY = "http://10.0.2.2/music/selectm.php";
    private BaseAdapter mAdapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        lv = getListView();
        lv.setDivider(null);

        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3) {

                Toast.makeText(CategoryActivity.this, "Item selected: " + position,
                        Toast.LENGTH_LONG).show();

                // Uncomment this to start a new Activity for a chosen item
                /* Intent i = new Intent(getApplicationContext(),
                        ItemListActivity.class);

                String category_id = ((TextView) view
                        .findViewById(R.id.category_id)).getText()
                        .toString();
                i.putExtra("category_id", category_id);

                startActivity(i);*/
            }
        });
        new LoadComments().execute();
    }

    class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CategoryActivity.this);
            pDialog.setMessage("Loading ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

            ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);
            try {
                JSONArray categories = json.getJSONArray(TAG_POSTS);
                for (int i = 0; i < categories.length(); i++) {
                    String id = categories.getJSONObject(i).getString("TAG_ID");
                    String name = categories.getJSONObject(i).getString("TAG_NAME");
                    String songs_count = categories.getJSONObject(i).getString("TAG_CATEGORIES_COUNT");
                    String category_logo = categories.getJSONObject(i).getString("TAG_CATEGORIES_LOGO");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_CATEGORIES_COUNT, songs_count);
                    map.put(TAG_CATEGORIES_LOGO, category_logo);

                    categoryList.add(map);
                }
            }catch (Throwable e){
                e.printStackTrace();
            }
            return categoryList;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            mAdapter = new CategoryListAdapter(CategoryActivity.this,result);
            lv.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }
    }

1 个答案:

答案 0 :(得分:1)

例如下面的代码刷新数据每分钟:

final Handler handler = new Handler();
Runnable refresh = new Runnable() {
        @Override
        public void run() {
            new LoadComments().execute();
            handler.postDelayed(this, 60 * 1000);
        }
    };
handler.postDelayed(refresh, 60 * 1000);