0

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.

Ammar
  • 169
  • 1
  • 5
  • 15
  • http://stackoverflow.com/questions/11323326/xml-parsing-in-ios-tutorial – Suhit Patil Nov 12 '13 at 10:07
  • thank you for response, but unfortunately my question not about XML parsing, if you please reread the question – Ammar Nov 12 '13 at 10:10
  • who handles the server side coding? – manujmv Nov 12 '13 at 10:13
  • I don't understand how you expect this question to be answered. Every server is unique and how you get a JSON response is not common knowledge for this web site. – trojanfoe Nov 12 '13 at 10:14
  • web service can return both XML and JSON, you need to communicate with the server side programmer to know which response is is coming form the server and accordingly parse it in client side. – Suhit Patil Nov 12 '13 at 10:15
  • there is a server built using Java , this server is responsible about handling the requests – Ammar Nov 12 '13 at 10:17
  • Mr.suhit is there a way to ask the server side to return JSON that I Know it could return either XML OR JSON. sorry maybe my question not clear enough – Ammar Nov 12 '13 at 10:20
  • Mr.trojanfoe maybe my question is not clear to you ... it is ok, but you can be more calm and understandable – Ammar Nov 12 '13 at 10:22

1 Answers1

3

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);

}
manujmv
  • 6,450
  • 1
  • 22
  • 35
  • I can explain more for you, the server had tested for android application and by default it return JSON to Android App, but in my IPhone app when I tried to get the resault it was in XML form then who determine the retutn type? – Ammar Nov 12 '13 at 10:25
  • 1
    are you sure there are no posting of variables in android app? – manujmv Nov 12 '13 at 10:27
  • Mr. @manujmv, after I so the android code I figured that there are postingvariables but I don't know how to code it using objective C and here is the android code: DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); HttpGet getRequest = new HttpGet(getBase() + str); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); result = getResult(response).toString();' – Ammar Nov 13 '13 at 09:38
  • 1
    @Ammar: pls find my edited answer – manujmv Nov 13 '13 at 10:03
  • @manujmv just a little more please I had error declaring (_parserSource ) in the following: '- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self._parserSource connectionFailed]; }' finally where I get my response data???? – Ammar Nov 13 '13 at 11:17
  • 1
    _parserSource is my delegate object. you dont need that. just remove it. you get your response in responseString in the connectionDidFinishLoading – manujmv Nov 13 '13 at 11:21
  • BIG BIG BIG thank you I am really grateful for your kind and understandable, wish you the best – Ammar Nov 13 '13 at 11:55
  • @Ammar:welcome bro. enjoy coding. is your problem completely solved? – manujmv Nov 13 '13 at 11:57
  • @Manujmv completely and as you were coding my application your self. unbeleivable :) kindly, can I directly mentioned you in my future question?? – Ammar Nov 13 '13 at 12:01