2017-04-27 6 views
-1

私はログインフォームを作成しました。フィールドr電子メールとパスワード。今私は特定のURLにフィールドからのデータをPOSTする方法を行うことができますしたい。私はIOSに全く新しいです。誰も私を助けることができますか? HTTPリクエストとJSONの解析方法Objective cでJSONサービスを使用してデータをPOSTする方法

+0

[iOSでHTTP POSTリクエストを送信する]の可能な重複(http://stackoverflow.com/questions/15749486/sending-an-http-postであります-request-on-ios) –

+0

あなたはRESTコールを使用していますか? – Arun

+2

[Jsonを使用してObjective Cのデータを投稿する]の可能性のある複製(0120-336-002) –

答えて

-1

ここコード、

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration 
defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setHTTPMethod:@"POST"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
// choose the right type for your value. 
NSDictionary *postDict = @{@"key1": value1, @"key2": value2}; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil]; 
[request setURL:[NSURL URLWithString:@"SERVER URL"]; 
[request setHTTPBody:postData]; 
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request 
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
    { 
     if (!error) {       
      NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      // Work with dictionary 
     } else { 
     // parsing error code here 
     } 
}]; 
[postDataTask resume]; 
-1
/*********See this**********/ 
-(void)webServiceCall{ 

NSString *dataToSend = [NSString stringWithFormat:@"Username=%@&Password=%@“,<userIdEnter Here>,<Password enter here>]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
NSString *Length = [NSString stringWithFormat:@"%d",[postData length]]; 

[request setURL:[NSURL URLWithString:@“WEBURL”]]; 
[request setHTTPMethod:@"POST"]; 

[request setValue:Length forHTTPHeaderField:@"Content-Length"]; 

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

[request setHTTPBody:postData]; 

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


} 
// check connection if you want 

/*****get response in delegates*******/ 



- (void)connection:(NSURLConnection *)connection didReceiveResponse: 
    (NSURLResponse *)response { 
    // A response has been received, this is where we initialize the instance var you created 
    // so that we can append data to it in the didReceiveData method 
    // Furthermore, this method is called each time there is a redirect so reinitializing it 
    // also serves to clear it 

    _responseData = [[NSMutableData alloc] init]; 

     } 
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data 
    { 
     /**************/ 
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 


    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                 options:kNilOptions 
                  error:&error]; 

    // NSArray* latestLoans = [json objectForKey:@"loans"]; 

    NSLog(@"json: %@", json); 

    [_responseData appendData:data]; 



    } 
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
NSLog(@"Error --> %@",error.localizedDescription); 
     /***************/ 
    } 
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; 

NSError *error = nil; 
     id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 
// use Result 
self.responseData = nil; 

    } 
+0

レスポンスの代理人には何を書きますか? @ajjjjjjj – omer

+0

真剣に私はコードを教えてくださいことを理解できないよね?このコードを書く場所と返答の代理人を書く場所はどこですか? @ajjjjjjj – omer

+0

まず、Webサービスを呼び出すクラスにコードを貼り付ける必要があります。 ** loginボタン**上のメソッド 'webServiceCall'を呼び出します。ここで、ユーザが入力したuser_idとパスワードを変更する必要があります。コールが行われた後、 'NSUrlConnection'デリゲートが呼び出されます(デリゲートドキュメントの読み込み)。 'connectionDidFinishLoading'で応答を得た後。このようにしてデータを解析する必要があります。 'id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 'その後、あなたは '結果' – ajjjjjjjj

関連する問題