0

我打算将我的网站转换为 iPhone 原生应用程序。我被困在如何实现这一点上。

最初,我开发了第一个屏幕,即我的应用程序的登录屏幕。我的服务器是用 Java 构建的。我能够将登录凭据发送到服务器并能够在服务器上看到请求。但是我无法收到服务器对我发送的请求的响应。

我的代码的一部分是:

NSString *post = @"username=";

    post = [post stringByAppendingString:username];
    post = [post stringByAppendingString:@"&password="];
    post = [post stringByAppendingString:password];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://mysite.com/login.action?"]];
    [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) 
    {
        receivedData = [[NSMutableData data] retain];
        NSLog(@"In \"LOGIN()\" :  receivedData = %@",receivedData);
    } 


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];

    NSLog(@"In \"didReceiveResponse()\" :  receivedData = %@",receivedData);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    NSString *ReturnStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"In \"didReceiveData()\" :  receivedData = %@",receivedData);
    NSLog(@"Return String : %@", ReturnStr);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);


    NSString *urlData;
    if (nil != receivedData) {
        urlData = [[[NSString alloc] initWithData:receivedData 
                                         encoding:NSUTF8StringEncoding] autorelease];
    }
    NSLog(@"In \"connectionDidFinishLoading()\" :  urlData = %@",urlData);

    [receivedData release];
}


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

    [connection release];

    [receivedData release];


    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
  1. 如何开发 LOGIN 应用程序?
  2. 我已成功向服务器发送请求,但无法从服务器获得响应。我该如何克服这个问题?
4

1 回答 1

1

如果您有 Web 开发经验,您可能会对 iPhone 版 NimbleKit 感兴趣。这是一个简单的 XCode 插件,允许您使用 HTML 和 JavaScript 开发整个原生应用程序。

于 2009-07-06T12:19:55.630 回答