我正在研究 Post 方法。我有 3 个 json 和 3 个字符串。我想将它们用于发布方法。 我尝试了很多解决方案,但都没有解决。 order_item_details, order_details, device_info 有json对象
NSURL *url=[NSURL URLWithString"my url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod"OST"];
NSMutableData *jsonData = [NSMutableData data];
// add params (all params are strings)
[jsonData appendData:[[NSString stringWithFormat"{order_item_details=%@", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat",order_details=%@", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat",transaction_id=%@", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat",order_unique_id=%@", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat",user_id=%@", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat",device_info=%@}", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
[request setValue:[NSString stringWithFormat"%d", jsonData.length] forHTTPHeaderField"Content-Length"];
// //HTTP Authentication
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"admin", @"1234"];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [authData base64Encoding];
[request setValue:[NSString stringWithFormat:@"Basic %@",authValue] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
}
作为回应,我得到了空值。我有像下面这样的json
{order_item_details= [{"item_amount":"500.00","item_price":"100.00","item_size":"M","page_name":"Billi","quantity":"5","fk_image_id":880},{"item_amount":"250.00","item_price":"50.00","item_size":"S","page_name":"Billi","quantity":"5","fk_image_id":880}],
order_details=[{"email":"[email protected]","mobile_order_date":"2016-02-11 11:44:16","orderUniqueId":"3204a5eb8be171a51057678233268718302","order_price":"750.00","order_total_amount":"800.00","order_type":"COD","paypal_transaction_id":"","service_charges":"40.00","service_percentage":"5.00","shipping_address":"asad","shipping_cost":"50.0","shipping_name":"asad","shipping_phone":"123456","fk_user_id":141,"no_of_items":2,"order_id":1}],
transaction_id=,
order_unique_id=3204a5eb8be171a51057678233268718302,
user_id=141,
device_info={"device":"klimtwifi","device_model":"SM-T700","device_brand":"samsung","device_id":"LRX22G","device_serial":"3204a5eb8be171a5","device_manufacturer":"samsung","device_product":"klimtwifixx"}}
我已经解决了这个问题,问题是服务器将数据视为一个对象。我已经用下面的代码改变了
NSMutableData *jsonData = [NSMutableData data];
[jsonData appendData:[[NSString stringWithFormat:@"order_item_details=%@&", orderItemDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_details=%@&", orderDetails] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"transaction_id=%@&", transID] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"order_unique_id=%@&", unique] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"user_id=%@&", userid] dataUsingEncoding:NSUTF8StringEncoding]];
[jsonData appendData:[[NSString stringWithFormat:@"device_info=%@", deviceInfo] dataUsingEncoding:NSUTF8StringEncoding]];
[request setURL:url];
关于ios - JSON POST Web 服务中的多个 Json 对象和 3 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35408138/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |