0

下面是我的源代码我的问题是如何从服务器更新数据库第一次应用程序在数据库中插入值 formserver 现在在服务器更新数据库一段时间后应用程序如何知道服务器更改数据库现在需要更新数据库?

     public class MainActivity extends Activity {
CategoryListAdapter3 cla;
static ArrayList<String> Category_ID = new ArrayList<String>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();


     private DbHelper mHelper;
private SQLiteDatabase dataBase;
private boolean isUpdate;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       mHelper=new DbHelper(this);
                dataBase=mHelper.getWritableDatabase();


      isInternetPresent = cd.isConnectingToInternet();

      // check for Internet status
      if (isInternetPresent) {
          // Internet Connection is Present
          // make HTTP requests
          new TheTask().execute();

      } else {
          // Internet connection is not present
          // Ask user to connect to Internet
          displayData();

      }



         void clearData() {
    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();

}

public class TheTask extends AsyncTask<Void, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(Void... arg0) {

SelectMenuAPI = "http://www.xyz/_webservices/mobile_api.php?response=getmaincategories";

        clearData();
        URL = SelectMenuAPI;
        URL2 = URL.replace(" ", "%20");

        try {

            Log.i("url", "" + URL2);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(URL2);
            HttpResponse response = client.execute(request);
            HttpEntity resEntity = response.getEntity();
            _response = EntityUtils.toString(resEntity);

        } catch (Exception e) {
            e.printStackTrace();
    //         IOConnect = 1;

        }
        return _response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);




        try {

            JSONObject json2 = new JSONObject(result);

            status = json2.getString("Status");
            if (status.equals("1")) {

                JSONArray school2 = json2.getJSONArray("data");
                //
                for (int i = 0; i < school2.length(); i++) {
                    JSONObject object = school2.getJSONObject(i);





                    String id = object.getString("category_id");
                    String name =object.getString("name");
                    String  image_path = object.getString("image_path");






                    dataBase=mHelper.getWritableDatabase();
                    ContentValues values=new ContentValues();

                    values.put(DbHelper.KEY_MYID,id);

                    values.put(DbHelper.KEY_FNAME,name);
                    values.put(DbHelper.KEY_LNAME,image_path );

                    System.out.println("");
                    if(isUpdate)
                    {    
                        //update database with new data 
                dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
                    }
                    else
                    {
                        //insert data into database
                        dataBase.insert(DbHelper.TABLE_NAME, null, values);
                    }
                    //close database
                    dataBase.close();


                }




                              else {





            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        displayData();
        }


             }


                      private void displayData() {
    dataBase = mHelper.getWritableDatabase();
    Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
            + DbHelper.TABLE_NAME, null);

    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();
    if (mCursor.moveToFirst()) {
        do {

     Category_ID.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MYID)));

      Category_name.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));

    Category_image.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));

        } while (mCursor.moveToNext());
    }
    gridview.setAdapter(cla);

    mCursor.close();
}






           public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="userdata";
public static final String TABLE_NAME="user";
public static final String KEY_FNAME="fname";
public static final String KEY_LNAME="lname";
public static final String KEY_ID="id";

public static final String KEY_MYID="myid";


public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY 
 KEY,"+KEY_MYID+" TEXT UNIQUE NOT NULL, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" BLOB)";
    db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);

}

      }
4

1 回答 1

1

有不同的方法可以解决这个问题,但是您可以在某些时间间隔内刷新您的应用程序。您可以每 X 分钟调用一次网络服务并询问是否有任何更改,或者您可以使用Google Cloud Messaging接收来自服务器的通知,然后刷新您的数据希望它有帮助:)

于 2013-11-01T06:58:21.507 回答