JSON将更适合Android应用。它比XML轻巧。
当您连接到服务器时。每个请求将通过webservice调用服务器。您可以在标头中以Base64编码形式发送身份验证。因此,每个请求都将在服务器中进行解析,并且可以在提供响应之前对凭据进行解码和身份验证。
要识别设备,您可以发送设备IME编号。您可以有一个表来跟踪登录到服务器的设备。
对于使用json的客户端中的base64身份验证。我还没有完成xml。
public static JSONObject SendHttpPost(Context context, JSONObject jsonObjSend) {
mPrefs = AppConfig.getPreferences(context);
String username = mPrefs.getString("UserName","");
String password = mPrefs.getString("Password","");
String host = mPrefs.getString("URL","");
String port = mPrefs.getString("Port","");
String url = "http:\\myapp.com\controller\getuser"
HttpResponse response = null ;
JSONObject jsonObjRecv =null;
try {
String usercredential = Utility.getB64Auth(username, password);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(url);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Authorization", usercredential);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
//Get hold of the response entity (-> the data):
httpentity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
Log.v(null, "resultString "+resultString);
instream.close();
// Transform the String into a JSONObject
if(resultString!=null){
jsonObjRecv = new JSONObject(resultString);
}
// Raw DEBUG output of our received JSON object:
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
}
} catch(SocketException se){
se.printStackTrace();
}catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
是,预设用户名和密码。使用偏好屏幕之类的屏幕进行设置。可以参考json.org来解析和创建json。是的,可以创建嵌套的json。
JSONObject body = new JSONObject();
JSONObject note = new JSONObject();
JSONObject commit = new JSONObject();
note.put("value", test2);
commit.put("create", note);
body.put("note", note);
body.put("commit", commit);