Android Json 解析
android 网络通信分两种 socket和http 常用http
Http请求数据方式有3种
(1)HttpUrlConnection;
(2)HttpClient;
(3)使用开源库如Volley
服务器返回数据一般有xml json html。常用json .
客服端和服务器数据交互方式有以下四种
HttpClient get
HttpClient post
URLConnection get
URLConnection post
四种方法都将获得待解析的一串字符(String),通常格式为json.
GET请求可以获取静态页面,也可以把参数放在URL字符串的后面,传递给服务器。POST与GET的不同之处在于POST的参数不是放在URL字符串里面,而是放在HTTP请求数据中
1. HttpUrlConnection Get 示例来自官方文档
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
2. URLConnection Post
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
// 此方法在正式链接之前设置才有效。
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
// 正式创建链接
httpConn.connect();
参考:http://www.devdiv.com/forum.php?mod=viewthread&tid=128975
3. HttpClient
A. 第一步是执行http连接获得响应数据
构造request
HttpGet request = new HttpGet(url);
或HttpPost request = new HttpPost(url);
得到HttpCient(接口)对象
DefaultHttpClient client = new DefaultHttpClient();
获得相应对象
HttpResponse response = client.excute(request);
//判断是否连接成功
if (response.getStatusLine().getStatusCOde() == 200) {
//获得响应
jsonStr = EntityUtils.toString(response.getEntity());
}
使用HttpPost可以用向服务器post数据
B.然后是解析获得的json格式数据
如: JSONObject json = new JSONObject(jsonStr);
String username = json.getString("username");
String password = json.getString("password");
其他JSON类:
C.向服务器发送json数据: 想构造JSONObject. 然后转成StringEntry.绑定到request上
如:
HttpPost request = new HttpPost(url);
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
param.put("name", "rarnu");
param.put("password", "123456");
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);
// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
// 生成 JSON 对象
JSONObject result = new JSONObject( retSrc);
String token = result.get("token");
关于http请求
http://www.open-open.com/lib/view/open1351324240738.html
http://blog.csdn.net/yesjava/article/details/11249287
关于json一些不错的文章
http://www.linuxidc.com/Linux/2013-03/80300.htm
http://blog.csdn.net/tianjf0514/article/details/7570607
http://www.cnblogs.com/xiaoluo501395377/p/3446605.html
http://www.cnblogs.com/xiaoluo501395377/p/3446605.html
http://www.cnblogs.com/tt_mc/archive/2011/01/04/1925327.html
http://www.open-open.com/lib/view/open1326376799874.html
http://blog.csdn.net/dadoneo/article/details/6233366
http://www.apkbus.com/android-52407-1-1.html
JSONObject 和JSONArray 解析
http://www.cnblogs.com/tt_mc/archive/2011/01/04/1925327.html
HttpURLConnection 和 HttpClient区别
http://www.cnblogs.com/devinzhang/archive/2012/01/17/2325092.html
http://www.cnblogs.com/devinzhang/archive/2012/01/17/2325092.html
HttpURLConnection各项设置
http://my.oschina.net/twobeings/blog/263010
post和get的区别
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。
建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
浙公网安备 33010602011771号