Je prévois de convertir mon site Web en une application native iPhone. Je suis coincé au point de savoir comment y parvenir. Dans un premier temps, j'ai développé le premier écran, c'est-à-dire l'écran LOGIN de mon application. Mon serveur est construit en Java. Je peux envoyer les identifiants de connexion au serveur et pouvoir voir la demande sur le serveur. Mais je suis incapable de recevoir la réponse du serveur pour la demande que j'ai envoyée.Comment puis-je créer un site Web en tant qu'application native iPhone?
Une partie de mon code est:
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]);
}
- Comment puis-je développer une application de connexion?
- J'envoie une requête au serveur mais je ne parviens pas à obtenir la réponse du serveur. Comment puis-je surmonter ce problème?