1.最近做的一个项目中传递的数据类型是Json格式的,所以在封装数据的时候要很清晰的表达传输数据的层次关系。在这里我简单的把自己封装成的一个比较规范的json,展示下!
这里我实在应用程序启动的时候就得到device的一些信息。
主要方法在这个Activity中。
package com.funo.jsonObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
public class json {
public static void onResume(Context paramContext) {
JSONObject localJSONObject = new JSONObject();
try {
localJSONObject.put("type", "launch");
localJSONObject.put("session_id", "dsds");
localJSONObject.put("date", "3=4-3");
localJSONObject.put("time", "23-3-3");
} catch (JSONException localJSONException) {
localJSONException.printStackTrace();
}
SendThread(paramContext, localJSONObject);
}
private static void SendThread(Context paramContext,
JSONObject paramJSONObject) {
JSONObject localJSONObject1 = getDeviceJson(paramContext);
JSONObject localJSONObject3 = new JSONObject();
JSONObject localJSONObject2 = new JSONObject();
JSONArray localJSONArray;
localJSONArray = new JSONArray();
localJSONArray.put(paramJSONObject);
try {
localJSONObject2.put("terminate", localJSONArray);
localJSONObject2.put("launch", localJSONArray);
localJSONObject3.put("body", localJSONObject2);
localJSONObject3.put("header", localJSONObject1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(localJSONObject3.toString());
}
private static JSONObject getDeviceJson(Context paramContext) {
JSONObject localJSONObject = new JSONObject();
try {
localJSONObject.put("idmd5", "idmd5");
localJSONObject.put("device_model", Build.MODEL);
localJSONObject.put("appkey", "appkey");
localJSONObject.put("sdk_type", "Android");
localJSONObject.put("sdk_version", "3.1.1");
localJSONObject.put("os", "Android");
localJSONObject.put("os_version", Build.VERSION.RELEASE);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return localJSONObject;
}
}
main.xml 使用者可以随便。不用当然也是可以的啦!
在下面会附上得到的结果,在配置文件记得加上一些权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.funo.jsonObject"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="JsonObjectActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
。
这里我实在应用程序启动的时候就得到device的一些信息。
主要方法在这个Activity中。
package com.funo.jsonObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
public class json {
public static void onResume(Context paramContext) {
JSONObject localJSONObject = new JSONObject();
try {
localJSONObject.put("type", "launch");
localJSONObject.put("session_id", "dsds");
localJSONObject.put("date", "3=4-3");
localJSONObject.put("time", "23-3-3");
} catch (JSONException localJSONException) {
localJSONException.printStackTrace();
}
SendThread(paramContext, localJSONObject);
}
private static void SendThread(Context paramContext,
JSONObject paramJSONObject) {
JSONObject localJSONObject1 = getDeviceJson(paramContext);
JSONObject localJSONObject3 = new JSONObject();
JSONObject localJSONObject2 = new JSONObject();
JSONArray localJSONArray;
localJSONArray = new JSONArray();
localJSONArray.put(paramJSONObject);
try {
localJSONObject2.put("terminate", localJSONArray);
localJSONObject2.put("launch", localJSONArray);
localJSONObject3.put("body", localJSONObject2);
localJSONObject3.put("header", localJSONObject1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(localJSONObject3.toString());
}
private static JSONObject getDeviceJson(Context paramContext) {
JSONObject localJSONObject = new JSONObject();
try {
localJSONObject.put("idmd5", "idmd5");
localJSONObject.put("device_model", Build.MODEL);
localJSONObject.put("appkey", "appkey");
localJSONObject.put("sdk_type", "Android");
localJSONObject.put("sdk_version", "3.1.1");
localJSONObject.put("os", "Android");
localJSONObject.put("os_version", Build.VERSION.RELEASE);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return localJSONObject;
}
}
main.xml 使用者可以随便。不用当然也是可以的啦!
在下面会附上得到的结果,在配置文件记得加上一些权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.funo.jsonObject"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="JsonObjectActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
。