tags:

views:

587

answers:

5

Hi all..

I am developing an application that connects to a remote web server and exchanges data with the web server frequently. First screen of my application provides login screen that authenticates user.

I am able to authenticate user on the web server by sending a request to the server but unable to get response from the server to display success alert to the user on the iphone. In clear, i am not getting any response.

The server I'm using is developed in JAVA.

I am using the following to send the request to server.

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 setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];

And,

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

}

This function is never called.

What should i do to receive response for the request i have sent?

A brief collection of my queries: Example: Login screen that validates a user in JAVA server

  1. I am sending request that i am able to see at the console of my server application.
  2. I'm unable to get response for the request.
  3. In which format the response data must be sent?

Can anyone help me in getting this done by guiding me to a clear picture of data exchange between iphone application and a Java servlet..

Any sort of help is much appreciated..

Thanks in advance.. Venkat

A: 

you should verify that the server is actually recieving the request from the iphone. a simple php script should return something.

<?php

    echo print_r($_GET, true);
    echo print_r($_POST, true);
?>

create a dummy app that makes a request to the script and displays the response

+1  A: 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSString *failureMessage = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
    NSLog(failureMessage);

}

Try implementing these three methods as well, you might be getting an error or no response. You can tell by using these methods.

Corey Floyd
Thank u guys for your quick response..I have implemented these three methods also. But no response from server.Can you guide me the process of developing LOGIN application that communicates with a JAVA server by requests and responses? I am stuck with communicating.
Venkat
A: 

Thank u guys for your quick response.. I have implemented these three methods also. But no response from server. Can you guide me the process of developing LOGIN application that communicates with a JAVA server by requests and responses? I am stuck with communicating.

Yeah, my server is receiving request from iphone. I am able to see the request that is sent to the server.

I am not able to understand how the data exchange between iphone application and JAVA server takes place. I'm a newbie to this iphone development.

Venkat
A: 

I'm not sure what authentication you refer to with LOGIN, but if the server requires HTTP Auth, you need to implement the delegate method

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
Andreas
A: 

I am using the following to send the request to server.

The code looks OK, but where is this code and what happens before and after the code you've shown us? Is it in a handler for a button press? Is it in a background thread?

For the asynchronous load you are using to work properly, you need to be on a thread with a default run loop, e.g. the main thread. And this code must return control to the run loop, i.e. must not busy wait doing something else.

Another thing to check is your server - is it in fact sending a response? If you send the same request from a browser, what content length do you get back?

Edit: it may help if you also show the java action handler on the server side. For example, does it send a HTTP redirect if the login is successful? And are you implementing the following delegate method:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

...and if so, what does that method look like. I'm wondering if you've maybe implemented this method and not got the right logic in there, thus stopped a redirect from happening.

frankodwyer
Thanks frankodwyer for taking time in looking my query.My code goes in a button click.I did not understand the terms, asynchronous load and run loop. Can you explain me in detail please.My server is receiving request from my application. I can see my request on my server's console. Do i need to implement JSON in iphone application?
Venkat
If your code is in a button click then you should be OK, these methods should be called if there is any data coming back. It sounds very likely that your server is really not sending any data in the response. And no, you don't need to implement JSON or at least that is not your problem here. Once you solve this issue you may want to look at JSON or plist format for sending your response (look at other SO questions about formats to use with an iphone app connecting to a webapp)
frankodwyer