Searcher_安然 2014-07-12 01:44
浏览 2707

IOS 向PHP服务器发送json数据 PHP服务器解析不到,格式有什么问题吗?

NSURL *url=[NSURL URLWithString:urlString];

NSMutableURLRequest *requests = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50];

[requests setHTTPMethod:@"POST"];

NSString *postParams = [NSString stringWithFormat:@"{\"token\":\"json\",\"account\":\"%@\",\"password\":\"%@\"}",username,password];
//NSDictionary *jsonDic=[NSDictionary dictionaryWithObjectsAndKeys:@"ios_json_data",@"token",@"testid",@"account",@"testid",@"password", nil];

//NSString *jsonStr=[NSString jsonStringWithDictionary:jsonDic];

NSData *data =[postParams dataUsingEncoding:NSUTF8StringEncoding];

[requests setValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
[requests setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];


[requests setHTTPBody:data];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:requests success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Success  \n======url===%@ \n====request.httpBody====%@   \n===response===%@  \n===json===%@",requests.URL,[[NSString alloc] initWithData:requests.HTTPBody encoding:NSUTF8StringEncoding],response,JSON);

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"falure============%@  %@  %@  %@",requests,response,error,JSON);
}];

[operation start];
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python