我已經做了一個登錄表單。領域r電子郵件和密碼。現在我想要將字段中的數據發佈到特定的url,它是如何完成的。我對IOS完全陌生。有誰能夠幫助我??如何做HTTP請求和JSON解析?如何在目標c中使用JSON服務POST數據
回答
下面是代碼,
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];
/*********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;
}
在響應代表中寫什麼? @ajjjjjjjj – omer
認真地,我不明白它可以詳細說明代碼嗎?在哪裏編寫代碼我們如何獲得響應以及在哪裏編寫響應委託? @ajjjjjjjj – omer
首先,您必須將代碼粘貼到要調用Web服務的類中。現在,在**登錄按鈕**上單擊,調用方法'webServiceCall',您必須更改用戶輸入的user_id和密碼。發生呼叫後,將會調用'NSUrlConnection'委託(讀取委託文檔)。在'connectionDidFinishLoading'中得到響應後。你必須以這種方式解析數據'id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; '。之後,你可以使用'結果' – ajjjjjjjj
- 1. 如何使用目標c中的json web服務?
- 2. 如何使用cURL將POST數據傳遞到JSON服務器
- 3. 如何將JSON數據對象POST到iOS7中的服務器
- 4. 在目標中使用POST c
- 5. 將數據POST到PHP的JSON服務
- 6. 分配數據從服務目標C
- 7. 從服務器使用jQuery POST函數返回JSON數據
- 8. 如何發送json數據使用C#中的RestSharp POST方法
- 9. 如何使用'POST'方法使用c#獲取JSON數據?
- 10. WCF Rest服務如何使用輸出參數訪問post JSON數據
- 11. 如何使用目標C調用Sharepoint Web服務GetListItems C
- 12. Android使用POST方法向服務器傳遞JSON數據
- 13. 如何發送JSON數據爲POST RESTful服務
- 14. 從多個參數調用目標C的JSON服務
- 15. JSON POST請求 - JSON數據,如何使用代理請求在跨服務器上使用JSON格式的數據發佈POST請求
- 16. iPhone:如何從Web服務獲取數據,其中Web服務使用JSON的「POST」方法?
- 17. 如何URL參數添加到JSON Web服務調用 - 目標C
- 18. 如何在目標c中接收來自APNS反饋服務器的數據
- 19. 如何在C中使用restfull web服務發送數據#
- 20. 如何將POST數據添加到REST服務的json請求正文中使用C#
- 21. 如何使用jQuery Post和Java在PostgreSQL中保存json數據?
- 22. 如何使用POST方法在XDR中發送JSON數據
- 23. 如何在Android中使用POST發送JSON數據?
- 24. 如何在OData服務中使用插入操作(POST)插入項目數組?
- 25. POST JSON數組體使用.NET中的web服務在Android中使用Volley
- 26. 使用drakma POST POST JSON數據:http-request
- 27. 如何使用json將數據發佈到web服務中swift
- 28. 如何使用JSON數據過去到Web服務器在Python
- 29. 如何使用Json發送數據並獲取在asp.net服務
- 30. 如何在服務器上使用AJAX發佈JSON數據
[iOS上發送的HTTP POST請求]的可能的複製(http://stackoverflow.com/questions/15749486/sending-an-http-post -request-on-ios) –
你在使用REST調用嗎? – Arun
[使用Json在Objective C中發佈數據]的可能副本(http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) –