0
我打算將我的網站轉換爲iPhone本機應用程序。我被困在如何實現這一點。如何製作一個網站作爲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]);
}
- 如何開發一個登錄應用程序?
- 我正在向服務器發送成功請求,但我無法從服務器獲得響應。我怎樣才能克服這個問題?