如何在ios objective c中将特定数据格式发布到rest wcf json服务
我需要发布以下类型的格式,并获取json数据如何在ios objective c中将特定数据格式发布到rest wcf json服务,ios,objective-c,iphone,json,wcf,Ios,Objective C,Iphone,Json,Wcf,我需要发布以下类型的格式,并获取json数据 { "Authentication": { "Username": "testUser@123", "Password": "testPassword@123" }, "RequestType": 4 } 我使用以下代码发布到wcf web服务 NSData *__jsonData = nil; NSString *__jsonString = nil; NSURL *ur
{
"Authentication": {
"Username": "testUser@123",
"Password": "testPassword@123"
},
"RequestType": 4
}
我使用以下代码发布到wcf web服务
NSData *__jsonData = nil;
NSString *__jsonString = nil;
NSURL *url=[NSURL URLWithString:@"http://adservicetest.azurewebsites.net/order/"];
dict = @{@"Authentication":@{@"Username":email.text,@"Password":password.text},@"FileID": [NSNumber numberWithInt:fileID],@"RequestType":[NSNumber numberWithInt:requestType]};
if([NSJSONSerialization isValidJSONObject:dict])
{
__jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
__jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Error %@", __jsonString);
}
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: __jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(@"Error %@", errorReturned);
}
else
{
NSString *responseString=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", responseString);
}
但是我得到了null responseString值,并且我已经使用chrome高级rest客户端测试了此post方法的工作情况,请纠正我哪里做错了。我在过去2天内无法解决此问题。请帮帮我
谢谢请检查下面的输出,并告诉我您的查询是否已解决。
请使用以下代码并注释您的代码,然后您可以将NSData转换为所需的数据格式。
NSString*responseString=[[NSString alloc]initWithBytes:[数据字节]长度:[数据长度]编码:NSUTF8StringEncoding];
NSLog(@“strAddr=%@”,responseString);
//NSString*responseString=[NSJSONSerialization JSONObjectWithData:数据选项:无错误:&errorReturned];
//NSLog(@“%@”,响应预算);
//NSLog(@“Error%@”,errorReturned);
打印responseString的说明:
服务
正文{color:#000000;背景色:白色;字体系列:Verdana;左边距:0px;右边距:0px;}内容{35;左边距:30px;字体大小:70em;底部填充:2em;}A:链接{color:#336699;字体重量:粗体;文本装饰:下划线;}A:已访问{color:#6699cc;字体重量:粗体;文本装饰:下划线;}A:活动{颜色:#336699;字体大小:粗体;文本装饰:下划线;}。标题1{背景颜色:#003366;边框底部:#336699 6px实心;颜色:#FFFFFFFF;字体系列:Tahoma;字体大小:26px;字体大小:正常;边距:0em 0em 10px-20px;填充底部:8px;填充左侧:30px;填充顶部:16px;}预处理{字体大小:小;背景色:#e5e5cc;填充:5px;字体系列:Courier New;页边距顶端:0px;边框:1px#f0f0e0实心;空白:预换行;空白:-预换行;字换行:中断字;}表格{边框折叠:折叠;边框间距:0px;字体系列:Verdana;}表格th{右边框:2px白色实心;边框底部:2px白色实心;字体大小:粗体;背景色:#cecf9c;}表格td{右边框:2px白色实心;边框底部:2px白色实心;背景色:#e5e5cc;}
服务
未找到终结点。请参阅有关构造对服务的有效请求的详细信息
请更新您的NSURL,并让我知道更多查询:)
答复如下:
Jason Data = {"Authentication":{"Password":"testPassword@123","Username":"testUser@123"},"RequestType":4}
2015-01-21 14:40:09.166 TestProject[2036:529966] strAddr = {"Errors":[],"StatusCode":200,"StatusMessage":"Get Orders List processed successfully","Files":[{"FileID":28,"Borrower":{"FirstName":"Ram","LastName":"Varma","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"5124848340"},"Email":"ram@vonexpy.com"},"CoBorrower":{"FirstName":"Nadeem","LastName":"Shaik","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"04065474405"},"Email":"nadeem@vonexpy.com"},"LenderDetails":{"LenderName":"VonExpy","LenderAddress":{"Address1":"101 ","Address2":"meghna ","City":"hyd","State":"AP","Zip":"500033"}},"LoanDetails":{"LoanType":"Housing","LoanPurpose":"business","LoanNumber":"VonExpy"},"PropertyAddress":{"Address1":"hyd","Address2":"guntur","City":"jubilee ","State":"UP","Zip":"87957"},"PropertyEstimatedValue":"fhy76","Orders":[{"OrderID":28,"AppraiserID":"27","AppraiserName":"FirstCLose","ProductDetails":{"ProductType":"hudy","FHACaseNumber":"746tr65"},"DueDate":"43543","InspectionDate":"983482374","Fee":"034202","NotesList":[],"Status":4,"Documents":[],"Condition":"987hkkk","ApplicationId":1,"ApplicationOrderId":"88"}]}]}
谢谢,但问题没有解决。我需要获取json数据,我不知道为什么会显示此消息。您能提供帮助吗?您好,当您尝试在浏览器中复制粘贴此url时,将显示Http页面,我们将得到相同的页面响应。您能分享有关您请求的更多详细信息,以便我可以帮助您吗?您需要更多帮助,请告诉我如何过滤此url上面的json数据使用状态,如果状态为4,相关数据应显示在tableview上,如果状态为5,相关数据应显示在tableview上。如何实现这一点。您需要使用NSJSONSerialization,它将返回您响应的字典或数组,然后您可以检查状态键的值是多少,并进一步使用它她
NSURL *url=[NSURL URLWithString:@"http://adservicetest.azurewebsites.net/order/json/process"];
Jason Data = {"Authentication":{"Password":"testPassword@123","Username":"testUser@123"},"RequestType":4}
2015-01-21 14:40:09.166 TestProject[2036:529966] strAddr = {"Errors":[],"StatusCode":200,"StatusMessage":"Get Orders List processed successfully","Files":[{"FileID":28,"Borrower":{"FirstName":"Ram","LastName":"Varma","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"5124848340"},"Email":"ram@vonexpy.com"},"CoBorrower":{"FirstName":"Nadeem","LastName":"Shaik","PhoneNumbers":{"HomePhone":"5122154434","WorkPhone":"04065474405"},"Email":"nadeem@vonexpy.com"},"LenderDetails":{"LenderName":"VonExpy","LenderAddress":{"Address1":"101 ","Address2":"meghna ","City":"hyd","State":"AP","Zip":"500033"}},"LoanDetails":{"LoanType":"Housing","LoanPurpose":"business","LoanNumber":"VonExpy"},"PropertyAddress":{"Address1":"hyd","Address2":"guntur","City":"jubilee ","State":"UP","Zip":"87957"},"PropertyEstimatedValue":"fhy76","Orders":[{"OrderID":28,"AppraiserID":"27","AppraiserName":"FirstCLose","ProductDetails":{"ProductType":"hudy","FHACaseNumber":"746tr65"},"DueDate":"43543","InspectionDate":"983482374","Fee":"034202","NotesList":[],"Status":4,"Documents":[],"Condition":"987hkkk","ApplicationId":1,"ApplicationOrderId":"88"}]}]}