IPhone sends Json Array via POST

I need to send the following JSON array via POST to our server:

task:{"id":"123","list":"456","done":1,"done_date":1305016383}

I tried using the JSON library , but it was kind of silly to use it. I even tried to create a POST-String myself, but also failed:

NSString *post = @"task='{id:123,list:456,done:1,done_date:1305016383}'";

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url 
                                               cachePolicy:NSURLRequestReloadIgnoringCacheData    
                                            timeoutInterval:30];

[request setHTTPMethod:@"POST"];    
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
....

Could you help me? For me, a literal POST line would be enough :)

+3
source share
3 answers

So this may or may not be the question you ask, but the JSON string is not formed correctly. An array of "tasks" in JSON format will look like this:

NSString *post = @"{"task":[{"id":"123","list":"456","done":1,"done_date":1305016383}]}";

PHP, , , , :

NSString *post = @"task[0][id]=123&task[0][list]=456&task[0][done]=1&task[0][done_date]=1305016383&";

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url 
                                           cachePolicy:NSURLRequestReloadIgnoringCacheData    
                                        timeoutInterval:30];

[request setHTTPMethod:@"POST"];    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
...

!

+1

:

-(void)imageFactory:(NSDictionary*)postJSON
{
    // Convert the JSON string to Data to be sent.
    NSData* postData = [[postJSON JSONRepresentation] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[URLManager imageFactory]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
    // IMPORTANT MAKE SURE YOU ADD THIS LINE SO THE SERVER KNOWS WHAT ITS GETTING
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    // Add some nice handlers so you know what you got from the server
    NSURLResponse* response;
    NSHTTPURLResponse* httpResponse;
    NSError* error;
    NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

    httpResponse = (NSHTTPURLResponse*) response;
    int statuscode = [httpResponse statusCode];

    if (statuscode == 200)
    {
        log4Debug(@"ImageFactory Response Successful, Retrieving image");
        // Handle the response here if needed
    }
    else
    {
        log4Error(@"ImageFactory Response Failed: %@", stringResponse);
        // Show some form of alert here if needed
    }
    // release all objects saved to memory
    [request release];
    request = nil;
    [stringResponse release];
    stringResponse = nil;
}

My json - , : { "user": 1337, "title": "Some title", "itemKeys": [1,1,1,1,1]}

+1

I believe the problem is not with the methods, but with the string you are trying to publish. Try the following:

NSString *post = @"\"task\":{\"id\":\"123\",\"list\":\"456\",\"done\":1,\"done_date\":1305016383}";

In other words: experiment with quotes.

What JSON library are you using?

0
source

All Articles