简体   繁体   English

在Android中同步

[英]SYNC in android

I am new in android Development . 我是android开发的新手。 I have to sync our local database with SQL Server . 我必须将本地数据库与SQL Server同步。

can the synchronization will done by SQLite or we need to use any other like CouchDB. 可以通过SQLite完成同步,还是需要使用其他任何方法(例如CouchDB)。

if I am using couch DB then the CouchDB synchronized with the SQLServer. 如果我使用Couch DB,则CouchDB与SQLServer同步。

Please provide any tutorial of SYNC. 请提供SYNC的任何教程。

Thanks in advance. 提前致谢。

Here i give the local database connection of a (ex)spinner in Android Application with MySql using php. 在这里,我给出了使用PHP的MySql在Android Application中使用(ex)spinner的本地数据库连接。

I hope it will helpful to you. 希望对您有帮助。

Refer this Code... 请参阅此代码...

MainActivity.java MainActivity.java

public class MainActivity extends Activity {


InputStream is=null;
String result=null;
String line=null;

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

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/spinner.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
}    

try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("Pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}    

try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
final String[] str1 = new String[JA.length()];       
final String[] str2 = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
str1[i] = json.getString("roll_no");
str2[i]=json.getString("name");
}


final Spinner sp = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();

for(int i=0;i<str2.length;i++)
{
list.add(str2[i]);
}

Collections.sort(list);

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(dataAdapter);

sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
{
// TODO Auto-generated method stub

String item=sp.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), item,Toast.LENGTH_LONG).show();
Log.e("Item",item);

}

@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}

});

}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}

spinner.php spinner.php

<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db='android';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$r=mysql_query("select * from class",$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
}
print(json_encode($cls));
mysql_close($con);
?>

Add below code to AndroidManifest.xml 将以下代码添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

Check the solution offered by fyreCloud Solutions . 检查fyreCloud Solutions提供的解决方案 This is source code for a demonstration Android application that implements MySQL replication between a MySQL server and the SQLite db on an Android device. 这是一个演示性Android应用程序的源代码,该应用程序在Android服务器上的MySQL服务器和SQLite数据库之间实现MySQL复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM