简体   繁体   中英

JSON response null using web service ios

i have successfully implemented registration and sign in and few other things using this method but i don't know why this isn't working .

I am sending this string to server:

p_no=phoneno&app_key=appkey&lat=22.5883049&lan=88.41021209999997&d_type=drivertype&c_type=cartype

using this code for connection

- (IBAction)menuPressed:(id)sender {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *phoneSaved = [userDefaults objectForKey:@"phoneSaveData"];
    NSString *appKeySaved = [userDefaults objectForKey:@"appKeySaveData"];
    if([phoneSaved length] == 0 || [appKeySaved length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign In"
                                                        message:@"Sign Up Properly"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    else {

        // string to post to web service
        NSString *postString = [NSString stringWithFormat:@"p_no=%@&app_key=%@&lat=%f&lan=%f&d_type=%d&c_type=%d",phoneSaved,appKeySaved,22.5883049,88.41021209999997,0,0];
        NSLog(@"%@",postString);
        //encode post string in supported encoding format in post data
        NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        //calculate length of the data to be sent
        NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

        NSURL *url = [NSURL URLWithString:@"http://103.227.62.106/srider-web/api/search_riders"];

        //URlRequest object and its initialization
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        //set the url for data sending
        [request setURL:url];

        //set HTTP method Post
        [request setHTTPMethod:@"POST"];

        //set HTTP header field with length of post data
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

        //set encoding value for HTTP header field
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        //set HTTP body or url request with postData
        [request setHTTPBody:postData];

        //create url connection object and initialize it with url request
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        //check if connection is made
        if (connection) {
            NSLog(@"Connection Successful");
        } else {
            NSLog(@"Connection Failed");
        }

    }

}

//method to receive data we get using post method
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"jsonResponseData : %@",jsonResponseData);

}
//method to receive error if connection is not made with server
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    // NSLog(@"%@",error);
}

//method to process data after connection has been made successfully
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //NSLog(@"%@",connection);
}

the data supposed to return is :

     {
"data": [
    {
        "d_id": "bZ",
        "lat": 0,
        "lan": 0,
        "d_type": "1",
        "c_type": "1",
        "status": "1",
        "driver_type_label": "",
        "car_type_label": "",
        "c_no": "",
        "color": "",
        "f_name": "Saikat",
        "l_name": "Singh",
        "waiting_time": 8,
        "base_fare": 20
    },
    {
        "d_id": "KlOr",
        "lat": "23.799999",
        "lan": "88.250000",
        "d_type": "1",
        "c_type": "1",
        "status": "1",
        "driver_type_label": "",
        "car_type_label": "",
        "c_no": "",
        "color": "",
        "f_name": "Sudipta",
        "l_name": "Khan",
        "waiting_time": 24,
        "base_fare": 20
    },
    {
        "d_id": "KlbG",
        "lat": "22.620001",
        "lan": "88.419998",
        "d_type": "1",
        "c_type": "1",
        "status": "1",
        "driver_type_label": "",
        "car_type_label": "",
        "c_no": "",
        "color": "",
        "f_name": "Saikat",
        "l_name": "Brando",
        "waiting_time": 14,
        "base_fare": 20
    },
    {
        "d_id": "mNCx",
        "lat": "22.490000",
        "lan": "88.315002",
        "d_type": "1",
        "c_type": "1",
        "status": "1",
        "driver_type_label": "",
        "car_type_label": "",
        "c_no": "",
        "color": "",
        "f_name": "Saikat",
        "l_name": "Roy",
        "waiting_time": 18,
        "base_fare": 20
    }
],
"count": 131
}

I would also love to know how to parse this data and access each one them values.

The Output is :

2015-09-16 16:11:47.859 sRider[19952:111620] p_no=5566788877&app_key=9da12dd458ec64f387f228ac20ddf356&lat=22.588305&lan=88.410212&d_type=0&c_type=0
2015-09-16 16:11:47.859 sRider[19952:111620] Connection Successful
2015-09-16 16:11:55.935 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.935 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.936 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.936 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.937 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.937 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.992 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.992 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.992 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.993 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.993 sRider[19952:111620] jsonResponseData : (null)
2015-09-16 16:11:55.994 sRider[19952:111620] jsonResponseData : (null)

I am totally a noob so i need core help, if you can't understand my question then please let me know i will provide more info but i really need help , been banging my head for 2 days .

it may be help you

create

NSMutableData *responseData;

change following code

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {

    responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
    //    NSLog(@"Did Receive Data %@", data);
    [responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
    NSLog(@"jsonResponseData : %@",jsonResponseData);
    NSDictionary * jsonResponseDict = [NSJSONSerialization JSONObjectWithData:nil options:kNilOptions error:nil];
    NSArray * jsonResponseDataArray = [jsonResponseDict objectForKey:@"data"];
}

You are facing problem because you are passing POST data in format of Query parameters. To pass data as POST parameters you should create NSDictionary of required parameters and then pass it as POST data.

// Create POST parameter dictionary
NSDictionary *dict = @{@"p_no":@"phoneno", @"app_key":@"appkey", @"lat":@"22.5883049", @"lan":@"88.41021209999997", @"d_type":@"drivertype", @"c_type":@"cartype"};

// Convert NSDictionary to NSData
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];

//set HTTP body or url request with postData
[request setHTTPBody:postData];

You can use this code and check error

NSError *error 
id jsonResponseData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; and check error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM