没有合适的资源?快使用搜索试试~ 我知道了~
首页android客户端从服务器端获取json数据并解析的实现代码
首先客户端从服务器端获取json数据 1、利用HttpUrlConnection 代码如下:/** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exception */ public static String readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream();
资源详情
资源评论
资源推荐
android客户端从服务器端获取客户端从服务器端获取json数据并解析的实现代码数据并解析的实现代码
首先客户端从服务器端获取json数据
1、利用HttpUrlConnection
代码如下:
/**
* 从指定的URL中获取数组
* @param urlPath
* @return
* @throws Exception
*/
public static String readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return new String(outStream.toByteArray());//通过out.Stream.toByteArray获取到写的数据
}
2、利用HttpClient
代码如下:
/**
* 访问数据库并返回JSON数据字符串
*
* @param params 向服务器端传的参数
* @param url
* @return
* @throws Exception
*/
public static String doPost(List<NameValuePair> params, String url)
throws Exception {
String result = null;
// 获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 新建HttpPost对象
HttpPost httpPost = new HttpPost(url);
if (params != null) {
// 设置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
}
/*// 连接超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
3000);*/
// 获取HttpResponse实例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), “UTF-8”);
} else {
Log.i(“HttpPost”, “HttpPost方式请求失败”);
}
return result;
}
其次Json数据解析:
json数据:
[{“id”:”67″,”biaoTi”:”G”,”logo”:”http://www.nuoter.com/wtserver/resources/upload/13508741845270.png”,”logoLunbo”:”http://www.nuoter.com/wtserver/resources/upload/13509015004480.jpg”,”yuanJia”:”0″,”xianJia”:”0″},
{“id”:”64″,”biaoTi”:”444″,”logo”:”http://www.nuoter.com/wtserver/resources/upload/13508741704400.png”,”logoLunbo”:”http://172.16.1.75:8080/wtserver/resources/upload/13508741738500.png”,”yuanJia”:”0″,”xianJia”:”0″},
{“id”:”62″,”biaoTi”:”jhadasd”,”logo”:”http://www.nuoter.com/wtserver/resources/upload/13508741500450.png”,”logoLunbo”:”http://172.16.1.75:8080/wtserver/resources/upload/13508741557450.png”,”yuanJia”:”1″,”xianJia”:”0″}]
代码如下:
/**
* 解析
*
* @throws JSONException
*/
private static ArrayList<HashMap<String, Object>> Analysis(String jsonStr)
throws JSONException {
/******************* 解析 ***********************/
JSONArray jsonArray = null;
// 初始化list数组对象
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 初始化map数组对象
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(“logo”, jsonObject.getString(“logo”));
map.put(“logoLunbo”, jsonObject.getString(“logoLunbo”));
map.put(“biaoTi”, jsonObject.getString(“biaoTi”));
map.put(“yuanJia”, jsonObject.getString(“yuanJia”));
map.put(“xianJia”, jsonObject.getString(“xianJia”));
map.put(“id”, jsonObject.getInt(“id”));
list.add(map);
weixin_38580959
- 粉丝: 3
- 资源: 961
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0