2

我一直致力于将 NSString 传递给 php 文件,我想要得到的结果是在 viewDidload 之后将 id 传递给 php 文件,这样我就可以使用 id 来查询 mysql 数据库并检索数据。有人可以教我怎么做吗?我看到很多人在下面讨论这些代码,但我不太确定它是关于什么的。

NSString * myString=@"1";
NSString * post = [[NSString alloc] initWithFormat:@"&id=%@", myString];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding  allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/getdata.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (conn) NSLog(@"Connection Successful");

这些代码是否适用于我的案例?感谢您的时间和建议。

4

1 回答 1

0

这是我为自己的一个应用程序做同样事情的方式:

*注意:这运行的是 GET 请求,而不是 POST 请求,但它在后台执行并高效运行

创建一个类来从服务器检索数据(我叫我的 online.h/m)

AppDelegate.h

//
//  AppDelegate.h
//  Faith Life Fellowship Streamer
//
//  Created by David Worth on 7/17/11.
//  Copyright 2011 Math Nerd Productions, LLC. All rights reserved.
//

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    NSOperationQueue *queue;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet ViewController *viewController;
@property (nonatomic, retain) NSString *receivedURL;

- (void)stringLoad:(NSString*)str;
+ (id)shared;
- (void)addOperation:(NSOperation*)operation;

@end

将这些部分添加到 AppDelegate.m

@synthesize receivedURL;

static AppDelegate *shared;

+ (id)shared;
{
    if (!shared) {
        [[AppDelegate alloc] init];
    }
    return shared;
}

- (id)init
{
    if (shared) {
        [self autorelease];
        return shared;
    }
    if (![super init]) return nil;

    queue = [[NSOperationQueue alloc] init];
    shared = self;
    return self;
}

- (void)addOperation:(NSOperation *)operation
{
    [queue addOperation:operation];
}

- (void)stringLoad:(NSString *)str
{
    self.receivedURL = str;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"string" object:nil];
}

在线.h

//
//  online.h
//  FLF Streamer
//
//  Created by David Worth on 4/30/12.
//  Copyright (c) 2012 Math Nerd Productions, LLC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AppDelegate.h"

@interface online : NSOperation

@property (nonatomic, retain) NSString* str;
@property (nonatomic) int type;

- (id)initWithString:(NSString*)url;

@end

在线.m

//
//  online.m
//  FLF Streamer
//
//  Created by David Worth on 4/30/12.
//  Copyright (c) 2012 Math Nerd Productions, LLC. All rights reserved.
//

#import "online.h"

@implementation online
@synthesize str;

- (id)initWithString:(NSString *)url
{
    if (![super init]) return nil;
    [self setStr:url];
    return self;
}

- (void)dealloc {
    [str release], str = nil;
    [super dealloc];
}

- (void)main {
    NSString *webpageString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:str] encoding:NSUTF8StringEncoding error:nil] autorelease];

    [[AppDelegate shared] performSelectorOnMainThread:@selector(stringLoad:)
                                                                         withObject:webpageString
                                                                      waitUntilDone:YES];
}

@end

然后在您的viewcontroller.m中调用它(将 mysite.com... 替换为您要访问的 PHP 页面的 URL):

online* o = [[online alloc] initWithString:@"http://www.mysite.com/myphppage.php?variable1=blah+blah+blah"];
AppDelegate* delegate = [AppDelegate shared];
[delegate addOperation:o];

在您的viewcontroller.m viewDidLoad 方法中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethodToProcessWhatTheServerReturns) name:@"string" object:nil];

然后在 viewController.m 中创建解析服务器返回内容的方法:

- (void)myMethodToProcessWhatTheServerReturns
{
     NSString* serverResponse = [AppDelegate shared].receivedURL;
     //Do stuff with serverResponse
}

快乐编码!

于 2012-09-07T14:39:50.893 回答