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 CreateBitmapFromWicBitmap内存释放问题。
    • ¥30 win c++ socket
    • ¥15 CanMv K210开发板实现功能
    • ¥15 C# datagridview 栏位进度
    • ¥15 vue3页面el-table页面数据过多
    • ¥100 vue3中融入gRPC-web
    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率