android发送/接收json数据
客户端向服务器端发送数据,这里用到了两种,一种是在url中带参数,一种是json数据发送方式;
url带参数的写法:
url+/?r=m/calendar/contact_list&uid=3&&subscriptionslist[pageindex]=10&subscriptionslist[recordlimit]=10
从“&”符号之后一连串都是参数。
发送方式代码编写:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(HttpUtil.BASIC_URL
+ HttpUtil.SUBSCRIPTION_URL);
try{
if (cookie != null) {
// httpClient.setCookieStore(LoginJsonUtil.cookie);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);nameValuePair.add(new BasicNameValuePair("uid",
uid));
nameValuePair.add(new BasicNameValuePair("subscriptionslist[pageindex]",
subscriptionslist_pageindex));
nameValuePair.add(new BasicNameValuePair("subscriptionslist[recordlimit]",
subscriptionslist_recordlimit));httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
传递参数使用nameValuePair。
如果使用cookie的话,使用上段代码中注释掉的部分
httpClient.setCookieStore(LoginJsonUtil.cookie);
使用json数据格式发送信息向服务器端:
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(BASIC_URL + url);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject.put("uemail", userbean.getEmail());
jsonObject.put("password", userbean.getPassword());
jsonObject2.put("userbean", jsonObject);
nameValuePair.add(new BasicNameValuePair("jsonString", jsonObject
.toString()));
Log.i("lifeweeker", jsonObject2.toString());
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
这里每个put的顺序我不清楚有没有规定,我是严格按照提供的前后顺序来组合json数据格式的。
前面我有用到android发送json数据;这里我想总结一下我用到的解析json数据格式的方式
json数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;
普通形式的:
服务器端返回的json数据格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
分析代码如下:
// TODO 状态处理 500 200
int res = 0;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
HttpResponse httpResponse = httpClient.execute(httpPost);
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String str2 = "";
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
builder.append(s);
}
Log.i("cat", ">>>>>>" + builder.toString());JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("userbean");String Uid;
String Showname;
String Avtar;
String State;Uid = jsonObject.getString("Uid");
Showname = jsonObject.getString("Showname");
Avtar = jsonObject.getString("Avtar");
State = jsonObject.getString("State");
带数组形式的:
服务器端返回的数据格式为:
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
}
分析代码如下:
// TODO 状态处理 500 200
int res = 0;
res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
if (res == 200) {
HttpResponse httpResponse = httpClient.execute(httpPost);
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String str2 = "";
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
builder.append(s);
}
Log.i("cat", ">>>>>>" + builder.toString());
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("calendar");
JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
CalendarInfo calendarInfo = new CalendarInfo();
calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
calendarInfo.setTitle(jsonObject2.getString("title"));
calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
calendarInfo.setShowtime(jsonObject2.getString("showtime"));
calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
calendarInfos.add(calendarInfo);
}
总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。
android发送/接收json数据的更多相关文章
- android发送/接收Json包含中文的处理
转自:http://wiki.neal365.com/2013/02/25/android%E5%8F%91%E9%80%81%E6%8E%A5%E6%94%B6json%E5%8C%85%E5%90 ...
- AJAX如何接收JSON数据
简介 在我们了解如何使用AJAX返回JSON数据的时候要先明白下列几点 1. JSON如何来表示对象的 2. JSON如何来表示数组的 var object = { "labId" ...
- ThinkPHP中使用ajax接收json数据的方法
本文实例讲述了ThinkPHP中使用ajax接收json数据的方法.分享给大家供大家参考.具体分析如下: 这里通过ThinkPHP+jquery实现ajax,扩展了下,写了个查询,前台代码如下: 首先 ...
- iOS key value coding kvc在接收json数据与 model封装中的使用
iOS key value coding kvc在接收json数据与 model封装中的使用 使用 kvc 能够极大的简化代码工作,及以后的接口维护工作: 1:先创建MovieModel类.h和 . ...
- 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据
ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关 新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({ t ...
- 后端接收json数据交互
学习记录,后端接收json数据几种方式 1.直接接收或者通过HttpServletRequest接收 public void test(String userid, HttpServletReques ...
- Beego框架POST请求接收JSON数据
原文: https://blog.csdn.net/Aaron_80726/article/details/83870563 ------------------------------------- ...
- .NET发送请求(get/post/http/https),携带json数据,接收json数据
C#发送https请求有一点要注意: ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateVa ...
- Jquery的$.ajax、$.get、$.post发送、接收JSON数据及回调函数用法
平时研究代码时,经常会遇到AJAX的相关用法,做项目时才真正体会到Ajax的强大之处(与服务器数据交互如此之便捷,更新DOM节点而不用刷新整个页面),以及运用的频繁程度.今天整理了一下自己之前没搞清楚 ...
随机推荐
- Java NIO示例:多人网络聊天室
一个多客户端聊天室,支持多客户端聊天,有如下功能: 功能1: 客户端通过Java NIO连接到服务端,支持多客户端的连接 功能2:客户端初次连接时,服务端提示输入昵称,如果昵称已经有人使用,提示重新输 ...
- android6.0 适配的问题——activity销毁的问题
1.最近我去运行我们公司所开发的APP,发现出现很多问题,就是从前一个页面跳到另外一个页面后,前一个页面会被销毁. 正常来说,activity跳转过程是这样: A: 存在intent +setActi ...
- Intellij IDEA常用快捷键——Mac版
http://blog.csdn.net/longshen747/article/details/17204699 http://totohust.iteye.com/blog/1035550 设置自 ...
- Unity Standard Assets 简介之 2D
这篇介绍2D资源包. 文件夹比较多,但是很多都是prefab的基础资源,所以我们只介绍 Prefabs 和 Scripts 文件夹. Prefabs文件夹: CharacterRobotBoy: 提供 ...
- 【CQgame】[下一百层] [Down]
简单的下一百层的c++实现,代码一晚上就码完了 注意:游戏前请在 默认值 或 属性 中调整缓冲区大小,否则会输出爆屏,方法写在代码里了 觉得速度 快/慢 的可以在第 23 行手动改一下,相信大神们能看 ...
- 编写unit test以及自动化测试WebDriver
http://msdn.microsoft.com/en-us/library/hh694602.aspx#BKMK_Quick_starts http://www.seleniumhq.org/ ...
- 通过Spring Mail Api发送邮件
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章. [Spring ...
- Android Properties 存储
1.初始化 private static void initProperties(){ File logFile = new File(Constants.PROGRESS_PROPERTIES); ...
- UICollectionView集合视图的概念
如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...
- App如何适应 iPhone 5s/6/6 Plus 三种屏幕的尺寸?
来自//www.cocoachina.com/ 初代 iPhone 2007 年,初代 iPhone 发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到 ...