从IOS端传递过来数据 java接受

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格式数据后,可以通过JsonObjectJsonArray类来转化,方便取出里面的值。这里就不再赘述,读者可自行百度。

转载于:https://my.oschina.net/zhangph89/blog/1553799

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值