Android客户端与服务器端的json数据交互(很详细)

来源:互联网 发布:淘宝吉他店铺介绍 编辑:程序博客网 时间:2024/05/01 09:30

Android客户端与服务器端的json数据交互,主要是通过json形式的数据交互,就是json的写入和解析。

 先看效果图,我最讨厌讲东西,一个图没有的。


算了,看来我不是写博客的材料,写不下去了,要排版之类的麻烦,大家还是直接去下载源码,里面有大量的注视,应该能看懂。 下载地址:源码下载地址http://download.csdn.net/detail/abc13939746593/4572693

登录界面:很传统的随便做了一下:



用的是android4.0的,别人说这样的EditText很有科技感。


注册界面:


也是很传统的,文本框之类的 。


下面看看包名,类名图。(忙着和比人聊天了,差点忘了) 现在继续

客户端的:


服务器端的:


下面贴上客户端的代码:

LoginRegisterActivity.java

[java] view plaincopy
  1. <span style="font-size:18px;color:#999900;">package com.gem.hsx.activity;  
  2.   
  3. import com.gem.hsx.operation.Operaton;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.ProgressDialog;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.Toast;  
  16.   
  17. //此为主activity的实现登录的。在android2.3以后,android规定了主activity不允许在主线程中做一些耗时较多的  
  18. //操作,包括网络的操作,主要是减少应用程序停止响应的问题。下面注释掉的部分是网上找到的方法,加上之后就可以在主线程中  
  19. //进行联网操作了,但是本人采用了线程的操作,未采用该方法  
  20. public class LoginRegisterActivity extends Activity {  
  21.     Button login;  
  22.     Button register;  
  23.     EditText etusername;  
  24.     EditText etpassword;  
  25.     String username;  
  26.     String password;  
  27.     ProgressDialog p;  
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.   
  33. //      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()         
  34. //      .detectDiskReads()         
  35. //      .detectDiskWrites()         
  36. //      .detectNetwork()   // or .detectAll() for all detectable problems         
  37. //      .penaltyLog()         
  38. //      .build());         
  39. //      StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()         
  40. //      .detectLeakedSqlLiteObjects()      
  41. //      .penaltyLog()         
  42. //      .penaltyDeath()         
  43. //      .build());     
  44.   
  45.   
  46.   
  47.         init();  
  48.         register.setOnClickListener(new RegisterOnclick());  
  49.         login.setOnClickListener(new LoginOnclick());  
  50.     }  
  51.     private void init()   
  52.     {  
  53.         etusername=(EditText) findViewById(R.id.etusername);  
  54.         etpassword=(EditText) findViewById(R.id.etpassword);  
  55.         login=(Button) findViewById(R.id.login);  
  56.         register=(Button) findViewById(R.id.register);  
  57.         p=new ProgressDialog(LoginRegisterActivity.this);  
  58.         p.setTitle("登录中");  
  59.         p.setMessage("登录中,马上就好");  
  60.     }  
  61.     private class RegisterOnclick implements OnClickListener  
  62.     {  
  63.         public void onClick(View v) {  
  64.             Intent intent=new Intent();  
  65.             intent.setClass(LoginRegisterActivity.this, Register.class);  
  66.             startActivity(intent);  
  67.         }  
  68.   
  69.     }  
  70.     private class LoginOnclick implements OnClickListener  
  71.     {  
  72.         public void onClick(View arg0) {  
  73.             username=etusername.getText().toString().trim();  
  74.             if (username==null||username.length()<=0)   
  75.             {         
  76.                 etusername.requestFocus();  
  77.                 etusername.setError("对不起,用户名不能为空");  
  78.                 return;  
  79.             }  
  80.             else   
  81.             {  
  82.                 username=etusername.getText().toString().trim();  
  83.             }  
  84.             password=etpassword.getText().toString().trim();  
  85.             if (password==null||password.length()<=0)   
  86.             {         
  87.                 etpassword.requestFocus();  
  88.                 etpassword.setError("对不起,密码不能为空");  
  89.                 return;  
  90.             }  
  91.             else   
  92.             {  
  93.                 password=etpassword.getText().toString().trim();  
  94.             }  
  95.             p.show();  
  96.             new Thread(new Runnable() {  
  97.   
  98.                 public void run() {  
  99.                     Operaton operaton=new Operaton();  
  100.                     String result=operaton.login("Login", username, password);                
  101.                     Message msg=new Message();  
  102.                     msg.obj=result;  
  103.                     handler.sendMessage(msg);  
  104.                 }  
  105.             }).start();  
  106.   
  107.         }  
  108.     }     
  109.     Handler handler=new Handler(){  
  110.         @Override  
  111.         public void handleMessage(Message msg) {  
  112.             String string=(String) msg.obj;  
  113.             p.dismiss();  
  114.             Toast.makeText(LoginRegisterActivity.this, string, 0).show();  
  115.             super.handleMessage(msg);  
  116.         }     
  117.     };  
  118.       
  119. }</span>  

Register.java

[java] view plaincopy
  1. <span style="font-size:18px;">package com.gem.hsx.activity;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.gem.hsx.bean.User;  
  11. import com.gem.hsx.json.WriteJson;  
  12. import com.gem.hsx.operation.Operaton;  
  13.   
  14. import android.app.Activity;  
  15. import android.app.AlertDialog;  
  16. import android.app.Dialog;  
  17. import android.app.ProgressDialog;  
  18. import android.content.DialogInterface;  
  19. import android.content.Intent;  
  20. import android.graphics.Bitmap;  
  21. import android.graphics.BitmapFactory;  
  22. import android.net.Uri;  
  23. import android.os.Bundle;  
  24. import android.os.Handler;  
  25. import android.os.Message;  
  26. import android.view.View;  
  27. import android.view.View.OnClickListener;  
  28. import android.view.View.OnFocusChangeListener;  
  29. import android.widget.Button;  
  30. import android.widget.EditText;  
  31. import android.widget.ImageView;  
  32. import android.widget.RadioButton;  
  33. import android.widget.Toast;  
  34.   
  35.   
  36. public class Register extends Activity {  
  37.     Button submit;  
  38.     Button select;  
  39.     EditText etusername;  
  40.     EditText etpassword;  
  41.     RadioButton ckman;  
  42.     RadioButton ckwoman;  
  43.     EditText etage;  
  44.     ImageView imgphoto;  
  45.     String str;  
  46.     String filepath=null;  
  47.     String jsonString=null;  
  48.     ProgressDialog dialog;  
  49.     private static final int REQUEST_EX = 1;  
  50.     String username=null;  
  51.     String password=null;  
  52.     String sex=null;  
  53.     String age=null;  
  54.     @Override  
  55.     protected void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.         setContentView(R.layout.register);  
  58.         init();  
  59.         //etusername.addTextChangedListener(new MyTextWatcher());  
  60.         etusername.setOnFocusChangeListener(new EtusernameOnFocusChange());  
  61.         select.setOnClickListener(new SelectOnclick());  
  62.         submit.setOnClickListener(new SubmitOnclick());  
  63.     }  
  64.     private void init()  
  65.     {  
  66.         submit=(Button) findViewById(R.id.submit);  
  67.         select=(Button) findViewById(R.id.select);  
  68.         etusername=(EditText) findViewById(R.id.etusername);  
  69.         etpassword=(EditText) findViewById(R.id.etpassword);  
  70.         ckman=(RadioButton) findViewById(R.id.ckman);  
  71.         ckwoman=(RadioButton) findViewById(R.id.ckwoman);  
  72.         etage=(EditText) findViewById(R.id.etage);  
  73.         imgphoto=(ImageView) findViewById(R.id.imgphoto);  
  74.         dialog=new ProgressDialog(Register.this);  
  75.         dialog.setTitle("上传数据中");  
  76.         dialog.setMessage("请稍等...");  
  77.     }  
  78.   
  79.     //  private   class MyTextWatcher implements TextWatcher  
  80.     //  {  
  81.     //  
  82.     //      public void afterTextChanged(Editable s) {  
  83.     //           str=etusername.getText().toString().trim();  
  84.     //           if (str==null||str.length()<=0)   
  85.     //           {  
  86.     //              etusername.setError("用户名不能为空");  
  87.     //           }  
  88.     //           else   
  89.     //           {    
  90.     //               new Thread(new Runnable() {  
  91.     //                    
  92.     //                  public void run() {  
  93.     //                       Operaton operaton=new Operaton();  
  94.     //                       String result= operaton.checkusername("Check", str);  
  95.     //                       Message message=new Message();  
  96.     //                       message.obj=result;  
  97.     //                       handler.sendMessage(message);  
  98.     //                  }  
  99.     //              }).start();  
  100.     //                 
  101.     //               
  102.     //            }  
  103.     //      }  
  104.     //      public void beforeTextChanged(CharSequence s, int start, int count,  
  105.     //              int after) {  
  106.     //            
  107.     //      }  
  108.     //  
  109.     //      public void onTextChanged(CharSequence s, int start, int before,  
  110.     //              int count) {  
  111.     //        
  112.     //      }  
  113.     //        
  114.     //  }  
  115.     //  Handler handler=new Handler()  
  116.     //  {  
  117.     //      @Override  
  118.     //      public void handleMessage(Message msg) {  
  119.     //          String msgobj=msg.obj.toString();  
  120.     //          if (msgobj=="该用户名可用")   
  121.     //          {  
  122.     //              etusername.setFocusable(false);  
  123.     //          }  
  124.     //          else   
  125.     //          {  
  126.     //              etusername.requestFocus();  
  127.     //              etusername.setError(msgobj);  
  128.     //          }         
  129.     //          super.handleMessage(msg);  
  130.     //      }     
  131.     //  };  
  132.     //    
  133.     private class EtusernameOnFocusChange implements OnFocusChangeListener  
  134.     {  
  135.         public void onFocusChange(View v, boolean hasFocus) {  
  136.             if (!etusername.hasFocus()) {  
  137.                 str=etusername.getText().toString().trim();  
  138.                 if (str==null||str.length()<=0)   
  139.                 {  
  140.                     etusername.setError("用户名不能为空");  
  141.                 }  
  142.                 else   
  143.                 {     
  144.                     new Thread(new Runnable() {  
  145.                  //如果用户名不为空,那么将用户名提交到服务器上进行验证,看用户名是否存在,就像JavaEE中利用  
  146.                 //ajax一样,虽然你看不到但是它却偷偷摸摸做了很多  
  147.                         public void run() {  
  148.                             Operaton operaton=new Operaton();  
  149.                             String result= operaton.checkusername("Check", str);  
  150.                             System.out.println("result:"+result);  
  151.                             Message message=new Message();  
  152.                             message.obj=result;  
  153.                             handler.sendMessage(message);  
  154.                         }  
  155.                     }).start();  
  156.                 }  
  157.             }  
  158.         }  
  159.     }  
  160.     Handler handler=new Handler()  
  161.     {  
  162.         @Override  
  163.         public void handleMessage(Message msg) {  
  164.             String msgobj=msg.obj.toString();  
  165.             System.out.println(msgobj);  
  166.             System.out.println(msgobj.length());  
  167.   
  168.             if (msgobj.equals("t")) {    
  169.                 etusername.requestFocus();  
  170.                 etusername.setError("用户名"+str+"已存在");  
  171.             }     
  172.             else   
  173.             {                         
  174.                 etpassword.requestFocus();  
  175.             }  
  176.             super.handleMessage(msg);  
  177.         }     
  178.     };  
  179.     private class SelectOnclick implements OnClickListener  
  180.     {  
  181.         public void onClick(View v) {  
  182.   
  183.             Intent intent = new Intent();  
  184.             intent.putExtra("explorer_title",  
  185.                     getString(R.string.dialog_read_from_dir));  
  186.             intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");  
  187.             intent.setClass(Register.this, ExDialog.class);  
  188.             startActivityForResult(intent, REQUEST_EX);  
  189.         }  
  190.     }  
  191.     protected void onActivityResult(int requestCode, int resultCode,  
  192.             Intent intent) {  
  193.         if (resultCode == RESULT_OK) {  
  194.   
  195.             Uri uri = intent.getData();  
  196.             filepath=uri.toString().substring(6);  
  197.             System.out.println(filepath);  
  198.             //用户的头像是不是图片格式  
  199.             if(filepath.endsWith("jpg")||filepath.endsWith("png"))  
  200.             {  
  201.                 File file=new File(filepath);  
  202.                 try {  
  203.                     InputStream inputStream=new FileInputStream(file);  
  204.                     Bitmap bitmap = BitmapFactory.decodeStream(inputStream);  
  205.                     imgphoto.setImageBitmap(bitmap);//如果是就将图片显示出来  
  206.                 } catch (FileNotFoundException e) {  
  207.                     e.printStackTrace();  
  208.                 }  
  209.                 submit.setClickable(true);  
  210.             }  
  211.             else  
  212.             {  
  213.                 submit.setClickable(false);  
  214.                 alert();  
  215.             }  
  216.   
  217.         }  
  218.     }  
  219.   
  220.     private class SubmitOnclick implements OnClickListener  
  221.     {  
  222.         public void onClick(View v) {  
  223.             username=etusername.getText().toString().trim();  
  224.             password=etpassword.getText().toString().trim();  
  225.   
  226.             if (ckman.isChecked()) {  
  227.                 sex="男";  
  228.             }  
  229.             else {  
  230.                 sex="女";  
  231.             }  
  232.             age=etage.getText().toString().trim();  
  233.             if (age==null||age.length()<=0)   
  234.             {    
  235.                 etage.requestFocus();  
  236.                 etage.setError("年龄不能为空");             
  237.                 return ;  
  238.             }  
  239.   
  240.             dialog.show();  
  241.             new Thread(new Runnable() {  
  242.   
  243.                 public void run() {  
  244.   
  245.                     Operaton operaton=new Operaton();  
  246.                     File file=new File(filepath);  
  247.                     String photo=operaton.uploadFile(file, "ImgReciver");  
  248.                     //先进行图片上传的操作,然后服务器返回图片保存在服务器的路径,  
  249.                     System.out.println("photo---->"+photo);  
  250.                     System.out.println("sex:------>"+sex);  
  251.                     User user=new User(username, password, sex, age,photo);  
  252.                     //构造一个user对象  
  253.                     List<User> list=new ArrayList<User>();  
  254.                     list.add(user);  
  255.                     WriteJson writeJson=new WriteJson();  
  256.                     //将user对象写出json形式字符串  
  257.                     jsonString= writeJson.getJsonData(list);  
  258.                     System.out.println(jsonString);   
  259.                     String result= operaton.UpData("Register", jsonString);  
  260.                     Message msg=new Message();  
  261.                     System.out.println("result---->"+result);  
  262.                     msg.obj=result;  
  263.                     handler1.sendMessage(msg);  
  264.                 }  
  265.             }).start();  
  266.   
  267.         }  
  268.     }  
  269.     private void alert()  
  270.     {  
  271.         Dialog dialog = new AlertDialog.Builder(this)  
  272.         .setTitle("提示")  
  273.         .setMessage("您选择的不是有效的图片")  
  274.         .setPositiveButton("确定",  
  275.                 new DialogInterface.OnClickListener() {  
  276.             public void onClick(DialogInterface dialog,  
  277.                     int which) {  
  278.                 filepath = null;  
  279.             }  
  280.         })  
  281.         .create();  
  282.         dialog.show();  
  283.     }  
  284.     Handler handler1=new Handler()  
  285.     {  
  286.         @Override  
  287.         public void handleMessage(Message msg) {  
  288.             dialog.dismiss();  
  289.             String msgobj=msg.obj.toString();  
  290.             if(msgobj.equals("t"))  
  291.             {  
  292.                 Toast.makeText(Register.this"注册成功"0).show();  
  293.                 Intent intent=new Intent();  
  294.                 intent.setClass(Register.this, LoginRegisterActivity.class);  
  295.                 startActivity(intent);  
  296.             }  
  297.             else {  
  298.                 Toast.makeText(Register.this"注册失败"0).show();  
  299.             }  
  300.             super.handleMessage(msg);  
  301.         }     
  302.     };  
  303. }</span>  
ExDialog.java


[java] view plaincopy
  1. package com.gem.hsx.activity;  
  2.   
  3.   
  4. import java.io.File;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10.   
  11. import android.app.ListActivity;  
  12. import android.content.Context;  
  13. import android.content.Intent;  
  14. import android.net.Uri;  
  15. import android.os.Bundle;  
  16. import android.util.Log;  
  17. import android.view.Display;  
  18. import android.view.LayoutInflater;  
  19. import android.view.View;  
  20. import android.view.ViewGroup;  
  21. import android.view.WindowManager;  
  22. import android.view.WindowManager.LayoutParams;  
  23. import android.widget.BaseAdapter;  
  24. import android.widget.ImageView;  
  25. import android.widget.ListView;  
  26. import android.widget.TextView;  
  27.   
  28.   
  29. import com.gem.hsx.activity.R;  
  30. //由于安卓模拟器里面没有文件浏览器,所以构建一个文件浏览器的对话框 主要是dialog和listview配合  
  31. //此是一个简单的浏览器主要目的是选择文件后获取文件的路径,借鉴了一些其他人的文件浏览器的写法  
  32. public class ExDialog extends ListActivity {  
  33.     private List<Map<String, Object>> mData;  
  34.     private String mDir = "/sdcard";  
  35.   
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.   
  41.   
  42.         Intent intent = this.getIntent();  
  43.         Bundle bl = intent.getExtras();  
  44.         String title = bl.getString("explorer_title");  
  45.         Uri uri = intent.getData();  
  46.         mDir = uri.getPath();  
  47.   
  48.   
  49.         setTitle(title);  
  50.         mData = getData();  
  51.         MyAdapter adapter = new MyAdapter(this);  
  52.         setListAdapter(adapter);  
  53.   
  54.   
  55.         WindowManager m = getWindowManager();  
  56.         Display d = m.getDefaultDisplay();  
  57.         LayoutParams p = getWindow().getAttributes();  
  58.         p.height = (int) (d.getHeight() * 0.8);  
  59.         p.width = (int) (d.getWidth() * 0.95);  
  60.         getWindow().setAttributes(p);  
  61.     }  
  62.   
  63.   
  64.     private List<Map<String, Object>> getData() {  
  65.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  66.         Map<String, Object> map = null;  
  67.         File f = new File(mDir);  
  68.         File[] files = f.listFiles();  
  69.   
  70.   
  71.         if (!mDir.equals("/sdcard")) {  
  72.             map = new HashMap<String, Object>();  
  73.             map.put("title""返回上一级目录/");  
  74.             map.put("info", f.getParent());  
  75.             map.put("img", R.drawable.ex_folder);  
  76.             list.add(map);  
  77.         }  
  78.         if (files != null) {  
  79.             for (int i = 0; i < files.length; i++) {  
  80.                 map = new HashMap<String, Object>();  
  81.                 map.put("title", files[i].getName());  
  82.                 map.put("info", files[i].getPath());  
  83.                 if (files[i].isDirectory())  
  84.                     map.put("img", R.drawable.ex_folder);  
  85.                 else  
  86.                     map.put("img", R.drawable.ex_doc);  
  87.                 list.add(map);  
  88.             }  
  89.         }  
  90.         return list;  
  91.     }  
  92.   
  93.   
  94.     @Override  
  95.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  96.         Log.d("MyListView4-click", (String) mData.get(position).get("info"));  
  97.         if ((Integer) mData.get(position).get("img") == R.drawable.ex_folder) {  
  98.             mDir = (String) mData.get(position).get("info");  
  99.             mData = getData();  
  100.             MyAdapter adapter = new MyAdapter(this);  
  101.             setListAdapter(adapter);  
  102.         } else {  
  103.             finishWithResult((String) mData.get(position).get("info"));  
  104.         }  
  105.     }  
  106.   
  107.   
  108.     public final class ViewHolder {  
  109.         public ImageView img;  
  110.         public TextView title;  
  111.         public TextView info;  
  112.     }  
  113.   
  114.   
  115.     public class MyAdapter extends BaseAdapter {  
  116.         private LayoutInflater mInflater;  
  117.   
  118.   
  119.         public MyAdapter(Context context) {  
  120.             this.mInflater = LayoutInflater.from(context);  
  121.         }  
  122.   
  123.   
  124.         public int getCount() {  
  125.             return mData.size();  
  126.         }  
  127.   
  128.   
  129.         public Object getItem(int arg0) {  
  130.             return null;  
  131.         }  
  132.   
  133.   
  134.         public long getItemId(int arg0) {  
  135.             return 0;  
  136.         }  
  137.   
  138.   
  139.         public View getView(int position, View convertView, ViewGroup parent) {  
  140.             ViewHolder holder = null;  
  141.             if (convertView == null) {  
  142.                 holder = new ViewHolder();  
  143.                 convertView = mInflater.inflate(R.layout.listview, null);  
  144.                 holder.img = (ImageView) convertView.findViewById(R.id.img);  
  145.                 holder.title = (TextView) convertView.findViewById(R.id.title);  
  146.                 //  holder.info = (TextView) convertView.findViewById(R.id.info);  
  147.                 convertView.setTag(holder);  
  148.             } else {  
  149.                 holder = (ViewHolder) convertView.getTag();  
  150.             }  
  151.   
  152.   
  153.             holder.img.setBackgroundResource((Integer) mData.get(position).get(  
  154.             "img"));  
  155.             holder.title.setText((String) mData.get(position).get("title"));  
  156.             //holder.info.setText((String) mData.get(position).get("info"));  
  157.             return convertView;  
  158.         }  
  159.     }  
  160.   
  161.   
  162.     private void finishWithResult(String path) {  
  163.         Bundle conData = new Bundle();  
  164.         conData.putString("results""Thanks Thanks");  
  165.         Intent intent = new Intent();  
  166.         intent.putExtras(conData);  
  167.         Uri startDir = Uri.fromFile(new File(path));  
  168.         intent.setDataAndType(startDir,  
  169.         "vnd.android.cursor.dir/lysesoft.andexplorer.file");  
  170.         setResult(RESULT_OK, intent);  
  171.         finish();  
  172.     }  
  173. };  

JsonUtil.java

[java] view plaincopy
  1. <pre name="code" class="java">package com.gem.hsx.json;  
  2.   
  3.   
  4. import java.lang.reflect.Type;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import com.google.gson.Gson;  
  8. import com.google.gson.reflect.TypeToken;  
  9.   
  10.   
  11.   
  12.   
  13. public class JsonUtil {  
  14.     public List<?> StringFromJson (String jsondata)  
  15.     {       
  16.         Type listType = new TypeToken<List<?>>(){}.getType();  
  17.         Gson gson=new Gson();  
  18.         ArrayList<?> list=gson.fromJson(jsondata, listType);  
  19.         return list;  
  20.   
  21.   
  22.     }  
  23. }</pre><br>  
  24. <pre></pre>  
  25. <p></p>  
  26. WriteJson.java  
  27. <p><span style="font-size:18px"></span></p>  
  28. <pre name="code" class="java">package com.gem.hsx.json;  
  29.   
  30. import java.util.List;  
  31. import com.google.gson.Gson;  
  32.   
  33. public class WriteJson {  
  34.     /* 
  35.      * 通过引入gson jar包 写入 json 数据 
  36.      */  
  37.     public String getJsonData(List<?> list)  
  38.     {  
  39. //此处要注意,时常会出现说找不到Gson类的情况,这时我们只需要将导入的包和系统提供换换顺序就行了  
  40.         Gson gson=new Gson();//利用google提供的gson将一个list集合写成json形式的字符串  
  41.         String jsonstring=gson.toJson(list);  
  42.         return jsonstring;  
  43.     }  
  44.     /* 
  45.      * 当然如果不用gson也可以用传统的方式进行写入json数据或者利用StringBuffer拼字符串 写成json字符串形式 
  46.      */  
  47. }</pre><br>  
  48. <br>  
  49. <br>  
  50. <br>  
  51. <br>  
  52. <br>  
  53. <p></p>  
  54. <br>  
  55. <pre></pre>  
  56. <pre></pre>

0 0
原创粉丝点击