user3040319 user3040319 - 4 months ago 33
iOS Question

How to post particular data format to rest wcf json services in ios objective c

i need to post below type of format,and getting the json data

{
"Authentication": {
"Username": "testUser@123",
"Password": "testPassword@123"
},
"RequestType": 4
}


and am using the following code to post to wcf web services,

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);
}


But am getting null responseString value and also i have tested this post method using chrome advanced rest client its working,please correct where am doing wrong.am unable to solve this issue from past 2 days. Please help me out.

Thanks

Answer

Please update your NSURL and please let me know more queries :)

NSURL *url=[NSURL URLWithString:@"http://adservicetest.azurewebsites.net/order/json/process"];

Response will be:

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"}]}]}
Comments