登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

iOS使用POST请求提交json数据不能被Java的Servlet后台读取,为什么?[复制链接]

自己用Java写了个Servlet后台...
然后写了个iOS的登录注册界面...

但是...

Servlet死活接收不到post请求传递的json数据...
Debug的时候收到的request的值都是null...
但iOS端测试这边request的httpbody是附上了json数据的...
就很迷茫...

所以...
有没有大手子帮我看看...

对了...
测试get请求是没有问题的...

iOS端大概代码如下...
<pre class="brush:objc; toolbar: true; auto-links: false;">

  • (IBAction)userLogin:(UIButton *)sender {
        [self.view endEditing:YES];

    
    //接口
    NSString *str = [NSString stringWithFormat:@"http://192.168.1.104:8080/BackStage/LoginServlet"];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //请求头
    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    
    NSDictionary *json = [[NSDictionary alloc] initWithObjectsAndKeys:usr, @"username", pwd, @"password", nil];
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    
    [request setHTTPBody:data];
    
    //发送请求
    NSURLSession *session = [NSURLSession sharedSession];
    
    //异步请求
    NSURLSessionDataTask task = [session dataTaskWithRequest:request completionHandler:^(NSData _Nullable data, NSURLResponse _Nullable response, NSError _Nullable error) {
        if (error) {
            NSLog(@"请求失败。失败原因:%@",error);
        }
        //解析返回数据
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    
    [task resume];
    
}
</pre>

这里是Java后台的Servlet代码...
<pre class="brush:java; toolbar: true; auto-links: false;">

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
                
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.login(username, password);
        
        Map<String, Object> map = new HashMap<String, Object>();
        if (user == null) {
            map.put("message", "登录失败,请检查用户名和密码是否正确");
            map.put("login", "false");
        } else {
            map.put("user", user);
            map.put("message", "登录成功,请稍后");
            map.put("login", "true");
        }

        JSONArray json = new JSONArray();
        json.put(map);
        
        PrintWriter out = response.getWriter();
        out.print(json.toString());
        out.flush();
    }
</pre>

shimakazepoi 405查看 · 2回复 · 2017-1-12 16:01 有用(0
共2个回答
IO10101 2017-1-13 10:18 回复

String username = request.getParameter("username");

    String password = request.getParameter("password");
        
        
        request是json,这么取不行
xsybloodwolf 2017-2-15 14:51 回复

NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];


[request setHTTPBody:data];
    
    这是把data对象设置进去啊,不是Jsion字符串
需要登入后才可以评论 登录 | 立即注册
*滑动验证:
关闭

每日头条

通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

返回顶部 返回版块