【问题标题】:How to display the datas in console from http get request in ios [closed]如何从ios中的http get请求在控制台中显示数据[关闭]
【发布时间】:2013-11-06 06:19:11
【问题描述】:

我正在处理 HTTP GET 和 POST 请求/响应。 它与 POST 一起工作正常,但坚持使用 GET 方法..

在我的控制台中,我得到一个 379 字节的数据。但是,我需要在我的控制台中显示整个数据(机器可读格式)。之后,我会将其转换为 JSON 格式。

注释行是 POST 方法。

这是我的代码..

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username", @"password"];
//    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
    [request setHTTPMethod:@"GET"];
//    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type" ];
//    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection Not Succeeded");
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 2" message:@"Error Occurred" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];
    NSLog(@"Error Description is here: %@", error.description);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];
//    NSLog(@"Displaying the Datas Received %@",);

}

我们非常感谢您的帮助。提前致谢。

【问题讨论】:

  • GET 请求有大小限制。为什么要使用 GET 而不是 POST ?
  • 要获得回复..我需要在我的控制台中查看这些数据
  • POST 请求也有响应。对 HTTP 请求有什么误解?
  • 在 GET 方法中,我们会得到响应,对吧?我需要在我的控制台中显示这些响应......明白了吗?
  • 将此行 NSLog(@"Displaying the Datas Received %@",data); 放入您的 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 委托方法中

标签: ios iphone objective-c get httprequest


【解决方案1】:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Displaying the Datas Received %@",data);
    NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Displaying the Datas Received In Readable Format %@",strResult);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];

}

【讨论】:

    【解决方案2】:

    使用 git 上可用的 ASI HTTP 库或 AFHTTPClient 或使用可以使用

    下面是发布请求的代码

    +(NSData *)postDataToUrl:(NSString*)urlString:(NSString*)jsonString
    {
        NSData* responseData = nil;
        NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        responseData = [NSMutableData data] ;
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        NSString *bodydata=[NSString stringWithFormat:@"data=%@",jsonString];
    
        [request setHTTPMethod:@"POST"];
        NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]];
        [request setHTTPBody:req];
        NSURLResponse* response;
        NSError* error = nil;
        responseData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&error];
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
        NSLog(@"the final output is:%@",responseString);
    
        return responseData;
    }
    

    【讨论】:

      【解决方案3】:

      如果您的数据太长,那么您将分块接收数据,并且委托didRecieveData: 将被自动调用多次。因此,您需要将收到的所有数据附加到NSMutableData 中。 创建一个NSMutableData 对象并在didRecieveData: 委托中接收数据时将数据附加到其中。

      -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
      {
          [myData appendData:data];
      }
      

      最后,当您的请求完成时,然后将该数据转换为字符串。

      -(void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
      
          NSString * stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
          [alert show];
          NSLog(@"Displaying the Datas Received %@", stringData);
      
      }
      

      【讨论】:

        【解决方案4】:

        我认为您必须完成 URL 请求的委托方法!代码应该看起来像

        @interface YourClass : UIViewController <NSURLConnectionDataDelegate> {
                NSMutableData *__downloadingData;
        }   
        
        //YourClass Delegation Handlers
        -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
        {
        
            if (!__downloadingData)
                __downloadingData = [NSMutableData dataWithCapacity:0];
        
            [__downloadingData appendData:data];
            //
        //        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
        //    [alert show];
        
        }
        
        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        
            NSLog(@"Data Received %@",__downloadingData);
            NSString *strResult = [[NSString alloc] initWithData:__downloadingData encoding:NSUTF8StringEncoding];
            NSLog(@"Data Received In Readable Format %@",strResult);
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-07-22
          • 2013-11-01
          • 2022-07-21
          • 2021-06-04
          • 2022-11-18
          • 1970-01-01
          • 2021-04-19
          相关资源
          最近更新 更多