0

I was trying to post data with a NSURLConnection. This is my code:

responseData = [[NSMutableData alloc]init];

self.regData=data;

NSString *postString = [NSString stringWithFormat:@"name=%@&email=%@&regId=%@unique_id=%@",self.regData.name,self.regData.email,self.regData.regId,UNIQUE_ID];

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url ];
NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
[req setValue:[NSString stringWithFormat:@"%d",[postData length]] forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:postData];
(void)[NSURLConnection connectionWithRequest:req delegate:self];

But, I received an exception like this

 NSInvalidArgumentException', reason: '-[MAAppDelegate connectionFailed]: unrecognized selector sent to instance 0x15d8b310'
*** First throw call stack:
(0x30fbaf53 0x3b2e46af 0x30fbe8e7 0x30fbd071 0x30f0c598 0x19cb83 0x318f43a3 0x318f42e7    0x318f5031 0x319804fd 0x30c280ef 0x30c26ae3 0x30c5831f 0x30eee719 0x30bbec3d 0x30bbeb0d 0x30bbe9a1 0x30f8618b 0x30f8565b 0x30f83e4f 0x30eeece7 0x30eeeacb 0x35bc7283 0x33790a41 0x187dbd 0x3b7ecab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

Help me solve this please.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Your title says "post JSON", but your code says "post x-www-form-urlencoded". Which is it? Also, have you implemented the `NSURLConnectionDataDelegate` methods in this object (which I'm gathering is being done in your app delegate)? – Rob Nov 02 '13 at 14:15
  • Looks like you're doing this in `MAAppDelegate` which is also set as the delegate of `NSURLConnection`. Try implementing a delegate method `connectionFailed` to see more details. And then you will encounter higher level problems that Rob is talking about. – Rok Jarc Nov 02 '13 at 14:20
  • 1
    actually am using json. i got this stuff from net. and also i successfuly used this for send data... any how could you help me with this code.. if u dnt mind pls... – Musadhikh Muhammed K Nov 02 '13 at 14:22
  • is that.. am not much familiar with that.. all i want to post my data to the api... its better if i get help with his code....:-( – Musadhikh Muhammed K Nov 02 '13 at 14:29
  • No problem. I've updated title and text of your question to eliminate confusion for future readers, the exception has nothing to do with JSON (nor does the code sample). – Rob Nov 02 '13 at 15:02

1 Answers1

1

A couple of thoughts:

  1. Your error message would appear to be unrelated to your code sample, but rather reporting that you tried to call a method called connectionFailed for your app delegate. I presume you have some code that tries to call a method by that name. You should update your question to include that code.

  2. You might want to add an exception breakpoint so it will identify the line of code that is causing this exception. That may simplify your diagnosis of the problem.

  3. As an aside, you are missing an ampersand before unique_id in your POST data, so this request won't work, even after you fix the above issue.

  4. You really should be percent escaping the parameters to your POST data. Maybe you don't have any characters that need escaping in your data, but to be safe, you really should be calling CFURLCreateStringByAddingPercentEscapes to percent escape your data. See this answer for an example NSString category that you can call to properly percent escape your data.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • actually i wrote those codes in delegate method of appdelegate. i moved my code to some where else. now there is no exception. thank u very much.... i really mean it... – Musadhikh Muhammed K Nov 02 '13 at 15:14
  • and also,i used these method for submitting data in my project. that works perfectly.. i never used percent escapes... some how it works..... – Musadhikh Muhammed K Nov 02 '13 at 15:17
  • @MusadhikhMuhammedK Yes, it will work until you encounter some special characters in one of those four fields. For example, if the `name` (or any of these fields) ever had an ampersand or plus sign it it, this existing code might not work. It's just a precaution that one should adopt in case a user enters in any special characters in their input. – Rob Nov 02 '13 at 15:20
  • 1
    @MusadhikhMuhammedK No, I was warning you about the ampersand, `&`. So, if the user's name was "Bill & Kate", the ampersand would be misinterpreted as prematurely terminating the field. Or, if it was "Bill + Kate", that `+` would be misinterpreted by your server as a space. Thus, when issuing requests that include any input provided by the user, you should generally go through a process of percent escaping with `CFURLCreateStringByAddingPercentEscapes`, which will, amongst other things, will replace the `&` with `%26`. Your server will automatically convert this back for you. – Rob Nov 02 '13 at 15:40