【问题标题】:How to POST data using JSON service in Objective c如何在 Objective c 中使用 JSON 服务发布数据
【发布时间】:2017-04-27 10:54:59
【问题描述】:

我已经制作了一个登录表单。字段 r 电子邮件和密码。现在我想将字段中的数据发布到特定的 url,它是如何完成的。我对IOS完全陌生。有谁能够帮助我??如何进行 HTTP 请求和 JSON 解析?

【问题讨论】:

  • 你在使用 REST 调用吗?
  • 您也是研究工具的新手?因为有很多问题询问如何做到这一点。此外,您可能希望提供有关您的 Web API 的详细信息(如果有特定的内容),展示您尝试过的内容等。

标签: ios objective-c xcode8


【解决方案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;

    }

【讨论】:

  • 在响应代表中写什么? @ajjjjjjjj
  • 说真的我不明白你能详细说明一下代码吗?在哪里编写此代码我们如何获得响应以及在哪里编写响应委托? @ajjjjjjjj
  • 首先,您必须将代码粘贴到要调用 Web 服务的类中。现在,在 login Button 上调用方法webServiceCall,您必须在其中更改用户输入的 user_id 和密码。调用发生后,将调用“NSUrlConnection”委托(阅读委托文档)。在connectionDidFinishLoading 得到回复后。您必须以这种方式解析数据` id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; `。之后,您可以使用“结果”
  • 你为什么降级?
  • 我没有。@ajjjjjjjj
猜你喜欢
  • 2012-04-10
  • 2016-09-07
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 2014-12-31
  • 1970-01-01
相关资源
最近更新 更多