iOS客户端发送json数据,java(servlet)服务器接受json数据
来源:互联网 发布:知耻近乎勇 作文 编辑:程序博客网 时间:2024/06/10 04:10
背景:
我们平时用Get方式发送http请求到服务器,服务器用request.getParameter(“xxx”)方法可以直接拿到值。用POST方式也是,可以直接拿到值,虽然Post方式前面只有服务器地址,参数是在Body部分,java服务器依然可以request.getParameter(“xxx”)的方式获取对应的值。实际开发中,后台给的接口各式各样,现在遇到iOS客户端发送json格式数据,java服务器接受iOS端传来的json格式数据。
iOS端实现思路:
iOS
端既然要传json
格式的数据,必然会封装成OC
字典。熟悉json
格式的人都知道,json
的大括号就是对应OC
字典,而json
的小括号对应OC
的数组。
第一步,iOS
端肯定要把所有要传的值全部封装成OC
字典的格式。
第二步,把封装好的OC
字典通过NSJSONSerialization
转化成NSData
。
第三步,把得到的NSData
再转成NSString
类型。
以上三步,说白了就是把要传输的值转成NSString
类型来传。那么,java
服务器自然就是字符串的形式来接收即可。
iOS端参考代码:
NSDictionary *jsonDict = @{@"stallInfo":@[ @{@"stallName":stallName,@"shopOneName":shopOneName,@"shopOneDes":shopOneDes,@"shopTwoName":shopTwoName,@"shopTwoDes":shopTwoDes}], @"longtitude":longtitude, @"latitude":latitude }; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:nil]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; [PublicAPI requestForPitchParam:jsonString callback:^(id obj) { }];
+(void)requestForPitchParam:(id)param callback:(ZFCallBack)callback{ NSString *path = @"http://192.168.1.101:8080/MoveStall/pitch"; NSMutableURLRequest *request = [PublicAPI setupURLRequestAndPath:path param:param requestMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [PublicAPI httpSession:request success:^(id responseOjb) { callback(responseOjb); } failure:^(NSError *error) { callback(error.localizedDescription); }];}
+(NSMutableURLRequest *)setupURLRequestAndPath:(NSString *)path param:(id)param requestMethod:(NSString *)requestMethod{ path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURL *url = [NSURL URLWithString:path]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; request.timeoutInterval = 30; request.HTTPMethod = requestMethod; return request;}
+(void)httpSession:(NSMutableURLRequest *)urlRequest success:(void(^)(id responseOjb))success failure:(void(^)(NSError *))failure{ NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error) { failure(error); } else { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; success(dict); } }] resume];}
Java服务器实现思路:
上面,已经阐述过了iOS
端实际是发送字符串,那么Java
服务器以接受字符串的方式来接收即可。而在这里,Java
服务器采用servlet
来编写。
Java服务器参考代码:
/** * 获取请求的 body * @param req * @return * @throws IOException */ public static String getRequestBody(HttpServletRequest req) throws IOException { BufferedReader reader = req.getReader(); String input = null; StringBuffer requestBody = new StringBuffer(); while((input = reader.readLine()) != null) { requestBody.append(input); } return requestBody.toString(); }
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); String jsonString = getRequestBody(request); System.out.println(jsonString); JSONObject jsonObj = JSONObject.fromObject(jsonString); System.out.println(jsonObj); }
Java
服务器接收到Json
格式数据后,可以通过JsonObject
、JsonArray
类来转化,方便取出里面的值。这里就不再赘述,读者可自行百度。
阅读全文
0 0
- iOS客户端发送json数据,java(servlet)服务器接受json数据
- java 接受 json 数据
- jQuery 向 servlet 发送和接受JSON数据。
- iOS开发-给服务器发送JSON数据
- 客户端发送Json数据问题
- 给服务器发送JSON数据
- 发送JSON数据给服务器
- 给服务器发送JSON数据
- 发送json数据给服务器
- 发送JSON数据到服务器
- 从jsp页面发送ajax请求,servlet接受参数并返回json数据
- 接受json格式数据
- SpringMVC接受JSON数据
- springMVC接受json数据
- Servlet解析JSON数据和发送JSON数据
- Servlet解析JSON数据和发送JSON数据
- iOS 学习日志 : 向服务器发送Json数据
- Android开发之与服务器(jsp)发送、接受JSON数据
- mybatis自动生成entity,dao和mapper
- 盗墓笔记之缘起与大结局∶张起灵与汪藏海的500年恩怨情仇
- ActiveMQ安装及启动遇到的问题
- SystemUI导航键显示隐藏某个按钮(Recent Home Back)
- CFNetwork internal error
- iOS客户端发送json数据,java(servlet)服务器接受json数据
- 白话快速排序
- 树莓派控制五线四相步进电机
- SQL Server巡检怎么做
- TTP223 触摸按键
- java中的换行原则
- TCP和UDP区别
- 获取浏览器信息
- Jquery中map函数的用法