2012-02-14 7 views
-1

私はサーバーで動作するはずのアプリケーションをやっています。Jsonがサーバにリクエストするにはどうすればいいですか?

ユーザー名とパスワードを使用してプログラムでサーバー上で認証する必要があります。

{ "ログイン": "mad_fashist"、 "パスワード": "eqeeuq313371"、 "メソッド": "authentificator"}

私はラインでサーバーにリクエストを行う必要があり 文字列はJsonになければなりません。認証が失敗した場合

受けて、私はラインを取得する必要があります:

{ "検証": "偽"、 "kuid": ""、 "SID": ""、 "UID": "" }

認証が渡された場合:

{ "検証": "真"、 "kuid": "6"、 "SID": "834fe9b4626502bf9ff23485a408ac40"、 "UID": "69"}

質問は送信方法と受信方法です上記のすべて?

答えて

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

関連する問題