2017-04-27 34 views
-1

我已經做了一個登錄表單。領域r電子郵件和密碼。現在我想要將字段中的數據發佈到特定的url,它是如何完成的。我對IOS完全陌生。有誰能夠幫助我??如何做HTTP請求和JSON解析?如何在目標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中發佈數據]的可能副本(http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) –

回答

-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

在響應代表中寫什麼? @ajjjjjjjj – omer

+0

認真地,我不明白它可以詳細說明代碼嗎?在哪裏編寫代碼我們如何獲得響應以及在哪裏編寫響應委託? @ajjjjjjjj – omer

+0

首先,您必須將代碼粘貼到要調用Web服務的類中。現在,在**登錄按鈕**上單擊,調用方法'webServiceCall',您必須更改用戶輸入的user_id和密碼。發生呼叫後,將會調用'NSUrlConnection'委託(讀取委託文檔)。在'connectionDidFinishLoading'中得到響應後。你必須以這種方式解析數據'id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; '。之後,你可以使用'結果' – ajjjjjjjj

相關問題