2013-03-15 4 views
0
-(void)makeSecondRequestWithQuestionID:(NSString*)questionID value:(NSString*)value andArray:(NSArray*)array{ 
NSURL * url = [NSURL URLWithString:URL]; 
ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:questionID forKey:@"question_id"]; 
[request setPostValue:value forKey:@"value"]; 
[request setPostValue:[array JSONRepresentation] forKey:@"array"]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

Мое значение - это массив, который изменяется при каждом нажатии кнопки действия. Как разместить этот массив? Благодаряразмещение json массива по URL-адресу

+0

Вы можете разместить весь код ???? В чем вы пытаетесь отправить данные? –

+0

Пожалуйста, проверьте код, который я разместил.thanks – Machete

ответ

0

Вы должны правильно установить HTTP заголовки для запроса POST:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/get-game"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    //this is your array. Init it as you need 
     NSArray *array; 
    //we need to create dict with your data and key 
     NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:array forKey:@"array"]; 
     NSError *err; 
    //data which will be actually sent to server in post request 
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONReadingAllowFragments error:&err]; 
    //now we need to tell that we send json data in our POST request. 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPBody:jsonData]; 
    //lets verify what will be sent  
     NSLog(@"request: %@", request); 
     NSLog(@"request headers: %@", [request allHTTPHeaderFields]); 
     NSLog(@"requet body: %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]); 
     //send it! 
     NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
    //continue with delegate methods for NSURLConnection 
Смежные вопросы