Objective C NSMutableURLRequest GET request returns null JSON

user1447300 picture user1447300 · Feb 17, 2014 · Viewed 7.5k times · Source

trying get Data from server using @"GET" request, this following code returns null all time:

is anybody finding an issue with my following code?

or it's can be server side Issue.. ?

thank for your help!

+ (id)sendParam:(NSString*)ParamString url:(NSString*)url{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];


    NSString *post = ParamString;

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

    [request setURL:[NSURL URLWithString:url]];


    NSLog(@"postLength =%@",postLength);

    [request setHTTPBody:postData];
    [request setHTTPMethod:@"GET"];
    [request addValue:ParamString forHTTPHeaderField:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];


    NSURLResponse *response;
    NSError *error;

    NSData *aData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSMutableArray*jsonReturn = [[NSMutableArray alloc]init];
    jsonReturn = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:aData options:kNilOptions error:&error];
    NSLog(@"jsonReturn %@",jsonReturn);

    return jsonReturn;
}

after Editing:

  + (id)sendParam:(NSString*)ParamString url:(NSString*)url{

    NSString*StringGETMETHOD = [NSString stringWithFormat:@"%@%@",url,ParamString];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:StringGETMETHOD]];
    [request setHTTPMethod:@"POST"];


    NSURLResponse *response;
    NSError *error;

    NSData *aData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"aData=%@",aData);


    if (aData) {

        jsonReturn=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:aData options:kNilOptions error:&error];

        NSLog(@"jsonReturn %@",jsonReturn);


}

Answer

user3182143 picture user3182143 · Feb 17, 2014

If you want to use GET for getting response from server just you can try in following method

//just give your URL instead of my URL
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;

NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&err];

 //You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.    

 //After that it depends upon the json format whether it is DICTIONARY or ARRAY 

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSArray *array=[[jsonArray objectForKey:@"search_api"]objectForKey:@"result"];