2012-06-16 5 views
0

Я разрабатываю приложение с полным веб-сервисом REST. Теперь у меня есть некоторые проблемы в этом случае, то есть строка, отправленная servier с использованием метода put. Я использую следующий код.отправка JSON на сервер с API REST

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://176.31.113.164/app/profil/add"]]; 
    [request setValue:@"Digest [email protected]:test" forHTTPHeaderField:@"Authorization"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 
    //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    NSDictionary *dic=[[NSDictionary alloc]init ]; 
     dic=[NSDictionary dictionaryWithObjectsAndKeys:@"15",@"@idUsers",@"17",@"@idProfils",@"ddduu",@"address",@"ddd",@"city",@"ddd",@"country",@"[email protected] <mailto:[email protected]>",@"email",@"1",@"enabled",@"fff55",@"firstName",@"fffdd3",@"lastName",@"fffffcc",@"nickName",@"test",@"password",@"0",@"phone",@"0",@"postCode",@"ROLE_USER",@"role", nil]; 
    NSString *jsonString = [jsonWriter stringWithObject:dic]; 
    NSData *postData= [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; 
    [request setHTTPBody:postData]; 
     [request setHTTPMethod:@"PUT"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

     NSURLConnection* _loginConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (_loginConnection==NULL) 
     { 
      NSLog(@"Problem"); 
     } 
    [_loginConnection start]; 

Полученные данные обрабатываются, как это:

NSString* tempString =[NSJSONSerialization 
        JSONObjectWithData:_receivedData 

        options:kNilOptions 
        error:&error]; 

ответ

0

Вы можете использовать ASIHTTP класс-обертку для выполнения операций REST в приложении IOS.

Загрузить последнюю версию ASIHTTP от here.

Смежные вопросы