2012-02-14 66 views
-1

我正在做一個應該與服務器一起工作的應用程序。如何使Json請求到服務器?

需要通過程序使用用戶名和密碼在服務器上進行授權。 我需要與線服務器的請求:

{ 「登錄」: 「mad_fashist」, 「密碼」: 「eqeeuq313371」, 「方法」: 「authentificator」}

的字符串必須在Json中。

對此,我應該得到一個行,如果驗證失敗:

{ 「驗證」: 「假」, 「kuid」: 「」, 「SID」: 「」, 「UID」: 「」 }

如果驗證通過:

{ 「確認」: 「真正的」, 「kuid」: 「6」, 「SID」: 「834fe9b4626502bf9ff23485a408ac40」, 「UID」: 「69」}

問題是如何發送和接收e以上所有?

回答

1

使用SBJson框架。並且也像水木清華:

-(void)requestProjects 
{ 


    //I prepare the string 

    NSString *preparedString=[NSString stringWithFormat:@"%@ %@", self.lastDate, self.currentCategory]; 
    NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:preparedString forKey:@"request"]; 

    //Prepare convert to json string 
    NSString *jsonRequest = [jsonDict JSONRepresentation]; 

    NSLog(@"jsonRequest is %@", jsonRequest); 

    //Set the URL YOU WILL PROVIDE 

    NSURL *url = [NSURL URLWithString:@"http:xxxxxxxxxxxxxxxxxxx"]; 

    //PREPARE the request 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    //Prepare data which will contain the json request string. 
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

    //Set the propreties of the request 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:jsonRequest forHTTPHeaderField:@"Query-string"]; 
    //set the data prepared 
    [request setHTTPBody: requestData]; 

    //Initialize the connection with request 
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
    //Start the connection 
    [delegate showIndicator]; 
    [connection start]; 

} 


//delegate methods: 

//METHODS TO HANßDLE RESPONSE 
#pragma mark NSURLConnection delegate methods 
//WHen receiving the response 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    NSLog(@" Did receive respone"); 
    [responseData setLength:0]; 


} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    //While receiving the response data 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    //When failed just log 
    [delegate hideIndicator]; 
    NSLog(@"Connection failed!"); 
    NSLog(@"Error %@", error); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    //When the response data is downloaded 
    // NSLog(@" Data obtained %@", responseData); 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    // NSLog(@" Response String %@", responseString); 
    //converted response json string to a simple NSdictionary 
    //If the response string is really JSONABLE I will have the data u sent me displayed succefully 

    NSMutableArray *results = [responseString JSONValue]; 
    NSLog(@"Response: %@", results); 
/// la la al alal alaa 
} 

這是你的後端誰應該定義一個JSON響應如何將看起來像

0

參考NSJsonSerialization類。這是新包含在ios5.0中的。這是您的需求中最靈活的一個。你可以在蘋果開發者網站上訪問它。 link to NSJSONSerialization

相關問題