objective-c - iOS 获取连接

标签 objective-c ios xcode nsurlconnection

这是我第一次访问这个网站,我对编码很陌生,所以我想知道是否有人可以帮助我。

我想设置一个从我的 iPhone 应用程序到我的网站的获取请求,并将信息从网站回传到我的手机。

我已经走了这么远,但不知道从这里去哪里。任何帮助将不胜感激,谢谢!

- (void)myData:(id)sender
{
  NSString *DataToBeSent;
  sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];
  [receivedData release];
  receivedData = [[NSMutableData alloc] init];
  DataToBeSent = [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
  [request setHTTPMethod: @"GET"];
  NSError *requestError;
  NSURLResponse *urlResponse = nil;  
  NSData *response1 = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponse error:&requestError];
  [dataToBeSent release];
}

老办法

- (void)myData:(id)sender
{
  NSString *dataToBeSent;
  sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];
  [receivedData release];
  receivedData= [[NSMutableData alloc] init];
  dataToBeSent= [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender];
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]];
  Theconn= [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
  NSLog (@"test1 %@", theRequest);
  NSLog (@"test2 %@", Theconn);
  [dataToBeSent release];
}

然后调用以下方法并获取我的数据,但是如果我在第一个请求之后发送另一个请求但在同一连接上发送不同的数据,它总是会给我相同的结果,这不应该发生

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  /* appends the new data to the received data */
  [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
  NSString *stringData= [[NSString alloc] 
  initWithData:receivedData encoding:NSUTF8StringEncoding]; 
  NSLog(@"Got data? %@", stringData);
  [self displayAlertCode:stringData];    
  [stringData release];
  // Do unbelievably cool stuff here //
}

最佳答案

假设您的数据已正确加载,您可以将数据转换为字符串并用它做任何您想做的事。

NSData *response1 = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponse error:&requestError];

//Make sure to set the correct encoding
NSString* responseString = [[NSString alloc] initWithData:response1 encoding:NSASCIIStringEncoding];

如果您的服务器返回 JSON,则有 3rd 方库可以将字符串解析为 NSArray 和 NSDictionary 等集合。如果您的服务器返回 XML,那么您可以使用 NSXMLParser。

编辑

我已经更改了您的代码,以稍微不同的方式管理内存。

.h

@property (nonatomic,retain) NSMutableData * receivedData;
@property (nonatomic,retain) NSURLConnection * Theconn;

.m

@synthesize receivedData;
@synthesize Theconn;

//A bunch of cool stuff


- (void)myData:(id)sender
{
    //If you already have a connection running stop the existing one
    if(self.Theconn != nil){
        [self.Theconn cancel];
    }

    sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];

    //This will release your old receivedData and give you a new one
    self.receivedData = [[[NSMutableData alloc] init] autorelease];


    NSString *dataToBeSent = [NSString stringWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?    Data=%@",sender];

    NSURLRequest *theRequest= [NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]];

    //This will release your old NSURLConnection and give you a new one
    self.Theconn = [NSURLConnection connectionWithRequest:theRequest delegate:self];

    NSLog (@"test1 %@", theRequest);
    NSLog (@"test2 %@", Theconn);

 }

 //...
 //Your delegate methods
 //...

 - (void) dealloc{
    [receivedData release];
    [Theconn release];
    [super dealloc];
 }

关于objective-c - iOS 获取连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246278/

相关文章:

iphone - 将自定义注释添加到 map 时使用

ios - 如何在 UIImagePickerController 捕获图像的瞬间获取当前位置?

xcode - 使用 TCP/IP 发送数据包以在 XCode 中使用

ios - 避免在 iOS 中找不到选择器

ios - 为什么 NSScanner 会丢弃空格?

ios - 在 react-native 中使用 iOS 启动屏幕作为应用程序背景

ios - Xcode 中的 "Dead Store"警告

iphone - AppDelegate 的 NSNumber 无法正确比较

ios - 在 iOS8 中无法获得正确的键盘高度值

ios - 以编程方式按下键盘按钮