2012-02-14 107 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