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];
3
  • 2
    Instead of passing nil in NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; pass a NSError* object and try to print the error. Commented May 24, 2013 at 4:53
  • Please verify your json using jsonlint.com There might be problem in json also. Commented May 24, 2013 at 4:54
  • your technique looks vulnerable. Error prone. Check my proper method here Commented May 28, 2013 at 9:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I already tried, but still no response..still no luck..please see my update code...
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?
hi, this is the response from the server { TopUpOnlineMessage = " - "; }
In your previous comment you said, that you still get no response but the string you posted looks like a perfectly valid JSON response?
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
|
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..

Comments

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.