public class JsonRequestUtils { public static Gson gson = null; // 声明gson对象 static { gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); // 创建gson对象,并设置日期格式 } }
记得导入gson 的jar包
public class RequestResult<T> { public static int Success = 1; // 成功 public static int UnKnow = 0; // 未知 public static int Faild = -1; // 失败 private int resultCode; // 状态码 [1:系统成功处理;负数表示系统没有成功处理] private String msg; // private T data; // 业务数据,状态码为1时,该数据才有效 public int getResultCode() { return resultCode; } public void setResultCode(int resultCode) { this.resultCode = resultCode; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
package com.example.bean; import java.io.Serializable; public class Recharge implements Serializable { private Integer id; private String addr; private String tel; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } }
package com.example.bean; import java.io.Serializable; import java.util.List; public class RechargeList implements Serializable { private int id; private List<Recharge> rList; public int getId() { return id; } public void setId(int id) { this.id = id; } public List<Recharge> getrList() { return rList; } public void setrList(List<Recharge> rList) { this.rList = rList; } }
package com.example.diancai; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import com.example.bean.Recharge; import com.example.bean.RechargeList; import com.google.gson.reflect.TypeToken; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); // initList(); } private void initData() { Recharge recharge = new Recharge(); recharge.setId(1); recharge.setAddr("武汉"); recharge.setTel("110"); RequestResult<Recharge> rr = new RequestResult<Recharge>(); rr.setData(recharge); String str = JsonRequestUtils.gson.toJson(rr); Log.v("test", str); TypeToken<RequestResult<Recharge>> typeToken = new TypeToken<RequestResult<Recharge>>() { }; RequestResult<Recharge> obj = JsonRequestUtils.gson.fromJson(str, typeToken.getType()); Log.v("test", "" + obj.getData().getAddr()); } private void initList() { Recharge recharge1 = new Recharge(); recharge1.setId(1); recharge1.setAddr("武汉"); recharge1.setTel("110"); Recharge recharge2 = new Recharge(); recharge2.setId(2); recharge2.setAddr("武汉"); recharge2.setTel("110"); List<Recharge> rList = new ArrayList<Recharge>(); rList.add(recharge1); rList.add(recharge2); RechargeList reList = new RechargeList(); reList.setrList(rList); String str = JsonRequestUtils.gson.toJson(reList); Log.v("test", str); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.diancai; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.view.LayoutInflater; import android.view.View; public class MyProgressDialog { Dialog dialog; Context context; public MyProgressDialog(Context context) { this.context = context; dialog = new Dialog(context, R.style.dialog); dialog.setOnCancelListener(onCancelListener); } public void initDialog(){ LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.myprogressdialog, null); dialog.setContentView(view); dialog.show(); } OnCancelListener onCancelListener = new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); } }; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你好" android:gravity="center"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点菜" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout>
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowBackground">@color/blue</item> </style>