在Android中的Web服务器和移动设备之间同步数据

时间:2015-03-24 08:55:07

标签: android web-services sqlite android-sqlite

我正在创建一个应用程序,其中所有数据都存储在服务器中。首次使用应用程序时,数据将以 SQLite 形式下载。然后,如果我稍后使用该应用程序,则Web服务需要调用的唯一时间是检查服务器中的数据是否更新。如果是,则更新的数据将添加到 SQLite 表中,如果不是,则将从内存中保存的 SQLite 数据库中提取数据。任何教程或代码都会有所帮助。

2 个答案:

答案 0 :(得分:0)

在下面的代码中,我通过php页面从在线服务器获取数据,数据填充在java数组中:

Class.java

try
        {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        httpclient = new DefaultHttpClient();

        httppost = new HttpPost("http://youronlinewebsite/yourpage.php");
        try
        {
        nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("Appname",appname));//Appname is the name of the column in your online database
        nameValuePairs.add(new BasicNameValuePair("Applink",applink));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
        response = httpclient.execute(httppost);


        if(response.getStatusLine().getStatusCode()==200)
        {
        entity=response.getEntity();
        if(entity !=null)
        {
            InputStream instream=entity.getContent();

            try
            {
                JSONArray jArray = new JSONArray(convertStreamToString(instream));
                arr=jArray;
                titlearray = new String[arr.length()];
                linkarray=new String[arr.length()];

                myDbHelper.openDataBase();              
        //// boucle for         
                for(int i =0; i<arr.length(); i++)
                {
                    try
                    {

        JSONObject json_data = arr.getJSONObject(i);

        titlearray[i]=json_data.getString("Appname");
        linkarray[i]=json_data.getString("Applink");
        //downloadFolder();     



 myDbHelper.Insertinfo(titlearray[i],linkarray[i]);//function that inserts data in sqlitedatabase


                 //     
                    }
                    catch(Exception ex)
                    {
                        a=ex.toString();
                    //  Toast.makeText(getActivity().getBaseContext(),ex.toString(),Toast.LENGTH_LONG).show();
                    }
                }


                myDbHelper.close();   //closing the database helper instance            
            }
            catch(Exception ex)
            {
                Toast.makeText(getActivity().getBaseContext(),ex.toString(),Toast.LENGTH_LONG).show();  
            }
        }
        else
        {

        }

        }
        else
        {

        }


        }
        catch(Exception ex)
        {
    //  a=ex.toString();    //Toast.makeText(getActivity().getBaseContext(),ex.toString(),Toast.LENGTH_LONG).show();
        }






        }
        catch(Exception ex)
        {

        }

功能convertstreamtostring

private static String convertStreamToString(InputStream is)
{


BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{

while ((line = reader.readLine()) != null) {
  sb.append(line + "\n");
}
}catch(IOException e)
{

}
finally
{
    try{
        is.close();
    }catch(IOException e)
    {

    }
    }return sb.toString();



}

答案 1 :(得分:0)

执行以下步骤:

  • 在您的在线数据库中创建一个名为update的字段,并在其中插入值&#34; 0&#34;

    • 在您的应用程序中,创建一个用于编写进程的服务 在您的应用程序的后台运行

    • 该过程将执行以下操作:它将检查您的在线数据库中的所有行,当您更新&#34;更新&#34;字段为0它不会进行任何更改,应用程序将从移动设备读取数据,否则如果有更改(字段中的值为1),它将从该行获取所有数据并在您的行中更新它SQLite数据库

    • 例如,每隔1分钟进行一次处理,请编写以下代码:

      Calendar cal = Calendar.getInstance();   Intent intent = new Intent(this,Updateservice.class);   PendingIntent pintent = PendingIntent.getService(this,0,intent,0);   AlarmManager alarm =(AlarmManager)getSystemService(Context.ALARM_SERVICE);   alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),60000,pintent);

不要忘记在清单文件中添加以下行:

<service android:name=".Updateservice" />
相关问题