Sunday, January 10, 2016

How to achieve android client and server data synchronization




            


I would like to achieve all the product information is cached locally, easy to use when there is no network. The product information contained in the introductory text of the product, product display pictures (multiple beds), Products screen.
Currently being considered by an incremental way to achieve data synchronization, at regular intervals to increment packaged as a zip file, but encountered a problem when product updates, delete pictures when, how to get to the modified images, screen data. Exhibit information table here should be how to design?
Reply:
Timely synchronized background on design synchronous interfaces, such as the front desk to Xintian, update, or delete operations are going to ask a server if the server returns have been added, updated or deleted, before going to operate the client local data.
Reply:
I know you said, the problem we encounter is how to manage a product to add, modify, delete pictures (picture multiple photos), published in packaging renewable resources to be able to know when those pictures, those products are modified too.
Reply:
public class JSONDemoServlet extends HttpServlet {
private static final long serialVersionUID = -7368225680407826408L;
private List & lt; User & gt; list;

/ **
* Data processing post submission of
* /
Override
protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet (request, response);
}

/ **
* Processing get data submitted
* /
Override
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType ("text / plain");
response.setCharacterEncoding ("UTF-8");
PrintWriter out = response.getWriter ();

// Prepare user data
prepareData ();

// JSON array
JSONArray array = new JSONArray ();

for (User bean: list) {
// A single user JSON object
JSONObject obj = new JSONObject ();
try {

obj.put ("id", bean.getId ());
obj.put ("name", bean.getName ());
obj.put ("email", bean.getEmail ());
obj.put ("gender", bean.getGender ());
} Catch (Exception e) {
// TODO: handle exception
}
array.put (obj);
}
// Output
out.write (array.toString ());
out.flush ();
out.close ();
}


private void prepareData ()
{
list = new ArrayList & lt; User & gt; ();
User bean1 = new User ();
bean1.setId (1001);
bean1.setName ("Tony");
bean1.setEmail ("tony@toeach.net");
bean1.setGender ("male");
list.add (bean1);

User bean2 = new User ();
bean2.setId (1002);
bean2.setName ("Jack");
bean2.setEmail ("jack@hotmail.com");
bean2.setGender ("male");
list.add (bean2);

User bean3 = new User ();
bean3.setId (1003);
bean3.setName ("Marry");
bean3.setEmail ("marry@163.com");
bean3.setGender ("female");
list.add (bean3);

User bean4 = new User ();
bean4.setId (1004);
bean4.setName ("Linda");
bean4.setEmail ("linda@21cn.com");
bean4.setGender ("female");
list.add (bean4);
}

}
User.java
package com.test.Demo;

public class User {
private int id;

private String name;

private String email;

private String gender;

public String getEmail () {
return email;
}

public void setEmail (String email) {
this.email = email;
}

public String getGender () {
return gender;
}

public void setGender (String gender) {
this.gender = gender;
}

public int getId () {
return id;
}

public void setId (int id) {
this.id = id;
}

public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

}
web.xml is configured to map the path:
& Lt;? Xml version = "1.0" encoding = "UTF-8" & gt;?
& Lt; web-app version = "2.5"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

& Lt; servlet & gt;
& Lt; servlet-name & gt; JSONDemoServlet & lt; / servlet-name & gt;
& Lt; servlet-class & gt; com.test.Demo.JSONDemoServlet & lt; / servlet-class & gt;
& Lt; / servlet & gt;

& Lt; servlet-mapping & gt;
& Lt; servlet-name & gt; JSONDemoServlet & lt; / servlet-name & gt;
& Lt; url-pattern & gt; / JSONDemoServlet & lt; / url-pattern & gt;
& Lt; / servlet-mapping & gt;


& Lt; welcome-file-list & gt;
& Lt; welcome-file & gt; index.jsp & lt; / welcome-file & gt;
& Lt; / welcome-file-list & gt;

& Lt; / web-app & gt;
Client:
package com.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
/ ** Called when the activity is first created. * /
Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);

try {
StringBuffer sb = new StringBuffer ();
// During the test, the machine is often used to do the test server, access to the machine's IP address to be set to 10.0.2.2
String url = "http://10.0.2.2:8080/article/JSONDemoServlet";
String body = getContent (url);
JSONArray array = new JSONArray (body);
for (int i = 0; i & lt; array.length (); i ++) {
JSONObject obj = array.getJSONObject (i);
.. sb.append ("id:") append (obj.getInt ("id")) append ("\ t");
.. sb.append ("name:") append (obj.getString ("name")) append ("\ r \ n");
sb.append ("gender:").. append (obj.getString ("gender")) append (
"\ T");
sb.append ("email:").. append (obj.getString ("email")) append (
"\ R \ n");
sb.append ("---------------------- \ r \ n");
}
TextView textView = (TextView) findViewById (R.id.textView);
textView.setText (sb.toString ());
} Catch (Exception e) {
// TODO: handle exception
}

}

private String getContent (String url) throws Exception {
StringBuilder sb = new StringBuilder ();
HttpClient client = new DefaultHttpClient ();
HttpParams httpParams = client.getParams ();

// Set network timeout parameters
HttpConnectionParams.setConnectionTimeout (httpParams, 3000);
HttpConnectionParams.setSoTimeout (httpParams, 5000);
HttpResponse response = client.execute (new HttpGet (url));
HttpEntity entity = response.getEntity ();
if (entity! = null) {
BufferedReader reader = new BufferedReader (new InputStreamReader (
entity.getContent (), "UTF-8"), 8192);
String line = null;
while ((line = reader.readLine ())! = null) {
sb.append (line + "\ n");
}
reader.close ();
}

return sb.toString ();
}

}
Reply:
Dude, synchronization problems finally have a solution? I am also now.
Reply:
The problems are too long, they will not provide the reply function.

No comments:

Post a Comment