Question

I am new to objectiveC, I have a problem in retrieving JSON type from a url, the url represent a web service which had built to return both JSON and XML, when I tried to consume the web service from the url the result was XML, what I need is to determine the type of returning data to be JSON in the request. by the way I am sure that the web service return both XML and JSON.

and here is my code where I get XML:

NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.1.1:8080/test2/eattel/restaurants"];
NSString *result=[[NSString alloc] initWithContentsOfURL:url];
NSLog(@"%@",result);

the result is :

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <restaurantImpls>
<Resstaurant Name="Abo Alshamat" PhoneNumber="0991992994" MobileNumber="0991992993" LastName="Shame" ID="1" FirstName="Samer"/>
<Resstaurant Name="AL-Kamal" PhoneNumber="0992993995" MobileNumber="0992993994" LastName="Ali" ID="2" FirstName="Ahmad"/>
<Resstaurant Name="Abo-MAhmoud" MobileNumber="0993377800" ID="12"/>
<Resstaurant Name="Four Season" PhoneNumber="0993994996" MobileNumber="0993994995" LastName="Ammar" ID="3" FirstName="William"/>
<Resstaurant Name="uuuuu" MobileNumber="0999555777" LastName="William" ID="20" FirstName="Ammar"/>
<Resstaurant Name="NewOneFromI2" MobileNumber="0999888777" ID="18"/>
<Resstaurant Name="JOURY" PhoneNumber="0999999998" MobileNumber="0999998997" ID="4"/>
<Resstaurant Name="TestTestRestaurant,Ammar,Hamed" MobileNumber="202020" ID="19"/>
</restaurantImpls>

thank you for your time.

Was it helpful?

Solution

We couldnt set the response data type from our request. Response is set from the server. From your description(web service return both XML and JSON), my guess is you need to post a status variable which showing the return status like isXML. It's only my guess. You need to contact server side programmers about the implementation of this request.

EDIT Try below code

responseData = [[NSMutableData alloc]init];
NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.1.1:8080/test2/eattel/restaurants"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
(void)[NSURLConnection connectionWithRequest:req delegate:self];

Then You need to implement NSURLConnection Delegates.

#pragma mark - NSURLConnection Delegates

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  {

}

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

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

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

    NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",responseString);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top