2 u013686641 u013686641 于 2014.07.12 09:44 提问

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];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!