android写入json
1. Android开发中为什么很少使用JSON存储数据
是可以用JSON存储数据对象的,而且也是Google推荐的,可以取代以实现Serializable来存储对象的方法。下面是使用JSON存储数据的原因。
Android开发中,涉及到对象存储,通常的做法是直接实现`Serializable`。有关这个接口,它保证了实现该接口的类的对象能够被`ObjectOutput/InputStream`直接输入输出,即序列化。这很方便,但是也很不好。
提到‘序列化’,大多数人都想到`Serializable`,而实际上‘序列化’的只是指“将对象的状态信息转换为可以存储或传输的形式的过程”,java的`Serializabe`是字节序列化的一种。
`Serialziable`的缺点之一是,实现了该接口的类将失去灵活性。这一点《Effective Java》第74条也指出了,实现了这个`Serializable`的类将会依赖这个类的内部演化,根源在于UID(Serial version UID)。如果你没有指定UID,那么每次这个类被序列化时都会根据这个类的当前状态生成一个UID。想象这么一种场景:这个类已经被导出了,比如发给其他公司或部门使用了,然后你又修改了这个类,那么当你再将这个类发布时,由于UID不同,其他公司或部门的程序员将可能得到一个“InvalidClassException”。
这种情况的根本原因是因为你不能控制序列化的实现,你控制不了UID的生成过程。这就需要一个自定义的序列化形式。在Android中,Google推荐JSON序列化。而且Android程序员也可以使用Gson等工具来进行序列化和反序列化。
和`Serializable`的字节序列化不同,JSON序列化是字符序列化。
此外,`Serializable`只适合存储对象。由于在传输时`Serializalbe`要做大量IO,Android提供了`Parcelable`。
最后,题主不应该把数据库和JSON,XML比较,如果要比,也只能把数据库和文件存储比。数据库适合存储数量大,关系复杂的数据,这样管理,查阅就很方便。与此相对文件存储适合数量小,关系简单的数据。
2. android怎么用gson做本地存储
Android存储文件通常可以用SharedPreferences、SQLite、Content Provider和File,但是SharedPreferences只支持简单的key-value,
通常,如果要存储一个对象,可以先把它序列化,然后用输入输出流存进file文件
另一个我比较喜欢的方式是:
写:先把一个对象用gson解析成json字符串(使用gson的toJson函数),然后当成一个value写进SharedPreferences里面
读:读取出来的时候就再次用gson把json解析成对象(使用gson的fromJson函数)
参考:
Android中的JSON详细总结
怎样使用Gson 解析 (deserialize) json字符串
Gson简要使用笔记
代码实现:
3. android用volley怎么给服务器发送json
1.下载官网的android SDK(本人用的是eclipse)
2.新建一个android项目:
File->new->andriod Application project
7、下面就是具体的使用post和get请求的代码:
A:发送get请求如下:
package com.example.xiaoyuantong;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
/**
* Demo
*/
public class MainActivity extends Activity {
private RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
TextView textView = (TextView)findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(this);
getJson();
textView.setText("hello");
}
private void getJson(){
String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!register.action?pwd='测试'";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//这里可以打印出接受到返回的json
Log.e("bbb", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// System.out.println("sorry,Error");
Log.e("aaa", arg0.toString());
}
});
requestQueue.add(jsonObjectRequest);
}
}
B:发送post请求如下:
package com.example.xiaoyuantong;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class PostActivity extends Activity {
private RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.post, menu);
return true;
}
private void init() {
TextView textView = (TextView)findViewById(R.id.postView);
requestQueue = Volley.newRequestQueue(this);
getJson();
textView.setText("hellopost");
}
private void getJson(){
String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!reg.action";
JsonObjectRequest jsonObjectRequest ;
JSONObject jsonObject=new JSONObject() ;
try {
jsonObject.put("name", "张三");
jsonObject.put("sex", "女");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//打印前台向后台要提交的post数据
Log.e("post",jsonObject.toString());
//发送post请求
try{
jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//打印请求后获取的json数据
Log.e("bbb", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// System.out.println("sorry,Error");
Log.e("aaa", arg0.toString());
}
});
requestQueue.add(jsonObjectRequest);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e + "");
}
requestQueue.start();
}
}
8、在android的logcat里面能查看到打印的请求
(红色的显示的是我在后台请求到数据)
有时候logcat显示不出数据,可能是消息被过滤了,可以在左边点击“减号”删除过滤
在server端,也就是在myeclipse的建立的另一个后台工程里面能获取到请求:
9、后续会补充json数据的解析部分,以及过度到移动云的部分,上面只是c/s模式下的一个简单的基于http的请求应答例子。