1

im struggling this for several hours, i want to send a json string to an asp.net server, but i always get null response. Is there anything wrong with my code?or problem in the server side? thanks a lot..

(url is hidden for security only), and this is the update code..

NSError *error;


NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK",@"BankAccountNo": @"123456789",@"Amount":@"1000000",
                           @"DepositFile":@"asset.PNG"};

NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.url.com"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url1];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
user2373192
  • 31
  • 1
  • 5
  • 2
    Instead of passing `nil` in `NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];` pass a `NSError*` object and try to print the error. – kaar3k May 24 '13 at 04:53
  • Please verify your json using http://jsonlint.com/ There might be problem in json also. – Prasad Devadiga May 24 '13 at 04:54
  • your technique looks vulnerable. Error prone. Check my proper method [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class/16771574#16771574) – Alphapico May 28 '13 at 09:45

3 Answers3

2

You could try to use Cocoa's built-in JSON serialization to obtain your JSON data instead of providing an escaped string yourself. This rules out improper escaping etc.

NSError* error = nil;
NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK"};
NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];

Please note, that I didn't specify all keys - You'd need to complete the dictionary before sending the request.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • I already tried, but still no response..still no luck..please see my update code... – user2373192 May 24 '13 at 06:49
  • Can you pass in an &error object to your sendSynchronousRequest call and log it to the console. Maybe the JSON is OK but the request has some other issues? – Thomas Zoechling May 24 '13 at 07:10
  • hi, this is the response from the server { TopUpOnlineMessage = " - "; } – user2373192 May 24 '13 at 07:26
  • In your previous comment you said, that you still get no response but the string you posted looks like a perfectly valid JSON response? – Thomas Zoechling May 24 '13 at 08:24
  • Hi weichsel, currently we tested by sending again the json string, so the response must be the string i sent, the fund code, totalamount, etc.if my code is ok, is there possibility the problem on the server?thanks – user2373192 May 24 '13 at 09:19
  • If both, your JSON & the HTTP request are valid and the error returned by NSURLConnection is nil, the problem is likely on the server side. – Thomas Zoechling May 24 '13 at 09:57
1

friend you are passing wrong method(as u sending it just as a NSString) for JSON use this method

NSError* errorJson = nil;
NSDictionary* jsonDict = @{@"FundCode": @(1), @"TotalAmount":@(1000000), @"PaymentType":@(0), @"Bank":@"AAA BANK",@"BankAccountNo": @"123456789",@"Amount":@@"1000000",
@"DepositFile":@"asset.PNG"};
NSData* postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&errorJson];

Hope this helps..

0

You can also use NSKeyArchiever to encode NSDictionary json to postData as given below

NSMutableDictionary *postDix=[[NSMutableDictionary alloc] init];
[postDix setValue:@"1" forKey:@"FundCode"];
[postDix setValue:@"1000000" forKey:@"TotalAmount"];
[postDix setValue:@"0" forKey:@"PaymentType"];    //etc

NSMutableData *postData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:postData];
[archiver encodeObject:postDix forKey:@"json"];
[archiver finishEncoding];
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90