2012-06-16 38 views
0

我正在開發一個應用程序與REST完整的Web服務。現在我有一些問題,在這種情況下,即字符串發送到一個服務器使用put方法。我正在使用下面的代碼。發送一個JSON到服務器與REST API

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包裝類來在iOS應用程序執行REST操作。

here下載最新的ASIHTTP。