0

Currently i am working in iPhone application, Using JSON to send a request and get response to read it.

Request (example url):

http://www.Genifer.com/index.php?q=api/username-available&username=stephen

I refer the Response from browser (Firefox):

{"status":true,"result":true}

I tried in xcode:

NSString *urlString = [NSString stringWithFormat:@" http://www.Genifer.com/index.php?"];
        NSString *parameter = [NSString stringWithFormat:@"q=api/username-available&username=stephen"];   

        NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSURL *url = [NSURL URLWithString:urlString];

        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

        [theRequest addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody:parameterData]; 

        NSURLConnection *connection = [[NSURLConnection alloc] 
                                       initWithRequest:theRequest delegate:self];

        if( connection )
        {
            mutableData = [[NSMutableData alloc] init];
        }
        else 
        {

        } 


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0]; 
    NSLog(@"mutableData:%@",mutableData);
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
    NSLog(@"mutableData:%@",mutableData);

}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [mutableData release];
    [connection release];
    return;
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


        NSString *jsonStr = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding]; 
        NSLog(@"JSonSTr : %@", jsonStr);

}

Response comes:

 JSonSTr : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

i refer the request in Firefox browser get the response like {"status":true,"result":true}. But i tried to integrate same request in xcode, but the response comes different, How to fix this? Please help me

Thanks in Advance

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
SampathKumar
  • 2,525
  • 8
  • 47
  • 82

1 Answers1

2

Can you check with this

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [jsonString JSONValue];

Check what values pop up in your dictionary by doing something like this

 NSDictionary *question = [jsonDict objectForKey:@"status"];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
S.P.
  • 3,054
  • 1
  • 19
  • 17
  • i have get the response from jsonString NSLOG - JSonSTr : – SampathKumar Oct 08 '12 at 20:57
  • Lets do this step by step I think the request that you are sending is not in JSOn that is why it is not sending you back a JSON. – S.P. Oct 08 '12 at 21:03
  • Here is a quick thing that you need to do to send a JSON request http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest The accept and content types should be JSON value – S.P. Oct 08 '12 at 21:04