 |
|
 |
|
 |
04-22-2009, 08:12 AM
|
#51 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 16
|
Problem passing xml data to web service
Quote:
Originally Posted by habdelra
|
Hello :
I used the code posted in this thread. It helped me a lot.
But now I am facing an issue please help me.
I want to send data to web service in the form of xml like
<son>
<child name="XYZ">
</son>
and get back some information related to child.
But 0 byte response is received please help me in fixing this issue
Thanks in advance
|
|
|
04-22-2009, 08:26 AM
|
#52 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 76
|
Quote:
Originally Posted by ashwanik04
Hello :
I used the code posted in this thread. It helped me a lot.
But now I am facing an issue please help me.
I want to send data to web service in the form of xml like
<son>
<child name="XYZ">
</son>
and get back some information related to child.
But 0 byte response is received please help me in fixing this issue
Thanks in advance
|
Use http post method here in IPhone to send data and add one aspx page in ur webservice and save that post data in to a xml file in page load of aspx page like this.
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
int streamLength;
int streamRead;
Stream s = Request.InputStream;
streamLength = Convert.ToInt32(s.Length);
Byte[] streamArray = new Byte[streamLength];
streamRead = s.Read(streamArray, 0, streamLength);
for (int i = 0; i < streamLength; i++)
{
sb.Append(Convert.ToChar(streamArray[i]));
}
string strFilePath = Request.PhysicalApplicationPath;
strFilePath = strFilePath + "\\iPhone.xml";
FileStream fs = new FileStream(strFilePath, FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
fs.Close();
}
cheers,
Gagandeep
|
|
|
04-22-2009, 08:31 AM
|
#53 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 16
|
Send xml data to web service
Quote:
Originally Posted by gagandeepb
Use http post method here in IPhone to send data and add one aspx page in ur webservice and save that post data in to a xml file in page load of aspx page like this.
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
int streamLength;
int streamRead;
Stream s = Request.InputStream;
streamLength = Convert.ToInt32(s.Length);
Byte[] streamArray = new Byte[streamLength];
streamRead = s.Read(streamArray, 0, streamLength);
for (int i = 0; i < streamLength; i++)
{
sb.Append(Convert.ToChar(streamArray[i]));
}
string strFilePath = Request.PhysicalApplicationPath;
strFilePath = strFilePath + "\\iPhone.xml";
FileStream fs = new FileStream(strFilePath, FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
fs.Close();
}
cheers,
Gagandeep
|
Thanks for your reply.
I forgot to say that the web service is external, I mean it cannot be changed.
So, is there any other way
|
|
|
04-22-2009, 08:35 AM
|
#54 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 76
|
Quote:
Originally Posted by ashwanik04
Thanks for your reply.
I forgot to say that the web service is external, I mean it cannot be changed.
So, is there any other way
|
what is the parameter type in your webservice method. Is it sting?
|
|
|
04-22-2009, 08:50 AM
|
#55 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 16
|
Quote:
Originally Posted by gagandeepb
what is the parameter type in your webservice method. Is it sting?
|
Yes it is string.
|
|
|
04-22-2009, 09:29 AM
|
#56 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Posts: 76
|
Quote:
Originally Posted by ashwanik04
Yes it is string.
|
Then see the post written by "nait" in this same thread he showed how to call soap web service methods with thier string parameter.
|
|
|
04-23-2009, 12:49 AM
|
#57 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 16
|
Order of calling web service
Quote:
Originally Posted by Trilitech
Like many others I wrote an app that was heavily dependent on asp.net SOAP web services that worked great in the simulator and then got hit with the surprise that the CoreServices library that contained all the web service functionality wasn't actually available on the iPhone. I'm still holding out hope this functionality will be added eventually.
All my code was dependant on receiving back the NSDictionary and NSArray object tree, and I didn't want to rewrite it all, so I created a wrapper to emulate the missing SOAP functionality by passing the request over REST. This allowed me to just swap out the method I was using for the webservice calls and keep the rest of the code the same. I thought I'd share it for anyone else in the same boat.
Fortunately asp.net already supports REST style requests (I didn't realize this before today), so no changes were needed on the web server side.
To demonstrate here the call to a simple method called "SayHello". You pass in name and it returns a single item called "string" that contains "Hello [yourname]". The code will work with more complex objects with child properties as well, and return them as NSDictionary and NSArray objects, but for simplicity I'm using "SayHello".
Code:
NSArray *keys = [NSArray arrayWithObjects:@"userName", nil];
NSArray *objects = [NSArray arrayWithObjects:@"jeremy", nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSDictionary *wsResponse=[WebServices callRestService:@"SayHello" :params];
NSString *responseString=[wsResponse objectForKey:@"string"];
callRestService is a helper method I created in a class called Webservices. This code was exactly the same before except the helper method was called callSoapService.
WebServices.m contains two methods. getRestUrl just appends all the method parameters into a url to make the REST request. callRestService uses the build-in NSXMLParser class to retrieve the xml and sends it to a custom class called XmlParser to handle the callbacks and create the NSDictionary result,
WebServices.m
Code:
+(id)callRestService: (NSString *) methodName : (NSDictionary *) params
{
NSURL *url=[WebServices getRestUrl: methodName : params];
XmlParser *xmlParser = [[XmlParser alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:xmlParser];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
return xmlParser.result;
}
+(NSURL *)getRestUrl: (NSString *) methodName : (NSDictionary *) params
{
NSString *url=@"http://services.mywebsite.com/mywebservice.asmx/";
url=[url stringByAppendingString:methodName];
BOOL firstKey=TRUE;
for (NSString *key in params)
{
NSString *value=[params objectForKey:key];
if (firstKey) url=[url stringByAppendingString:@"?"]; else url=[url stringByAppendingString:@"&"];
url=[url stringByAppendingString:key];
url=[url stringByAppendingString:@"="];
url=[url stringByAppendingString:value];
firstKey=FALSE;
}
return [NSURL URLWithString:url];
}
The final step is to create the XmlParser handler. This class keeps track of each open and close tag to build an object tree containing NSMutableArray NSMutableDictionary and NSString objects.
XmlParser.h
Code:
#import <Foundation/Foundation.h>
@interface XmlParser : NSObject {
NSMutableDictionary *result;
NSString *currentElementName;
NSString *currentElementValue;
NSMutableArray *parentArray;
}
@property (nonatomic, retain) NSMutableDictionary *result;
@property (nonatomic, retain) NSString *currentElementName;
@property (nonatomic, retain) NSString *currentElementValue;
@property (nonatomic, retain) NSMutableArray *parentArray;
- (void)parserDidStartDocument:(NSXMLParser *)parser;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
@end
XmlParser.m
Code:
#import "XmlParser.h"
@implementation XmlParser
@synthesize result;
@synthesize currentElementName;
@synthesize currentElementValue;
@synthesize parentArray;
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
result=[[NSMutableDictionary alloc] init];
parentArray=[[NSMutableArray alloc] init];
[parentArray addObject:result];
currentElementName=@"";
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if (qName) {
elementName = qName;
}
currentElementValue=@"";
if (currentElementName!=@"")
{
id newParent=NULL;
if ([currentElementName isLike:@"*Array*"])
{
newParent=[[NSMutableArray alloc] init];
} else {
newParent=[[NSMutableDictionary alloc] init];
}
[parentArray addObject:newParent];
}
currentElementName=elementName;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (qName) {
elementName = qName;
}
if (currentElementName==@"")
{
//We're adding a container with children. Add it to the parent and remove this item fromt he parentArray
int currentIndex=[parentArray count]-1;
int parentIndex=currentIndex - 1;
id currentChild=[parentArray objectAtIndex:currentIndex];
id currentParent=[parentArray objectAtIndex:parentIndex];
if ([currentParent isKindOfClass:[NSMutableArray class]])
{
[currentParent addObject:currentChild];
} else {
[currentParent setObject:currentChild forKey:elementName];
}
[parentArray removeObjectAtIndex:currentIndex];
} else {
//We're adding a simple type element
int currentIndex=[parentArray count]-1;
id currentParent=[parentArray objectAtIndex:currentIndex];
if ([currentParent isKindOfClass:[NSMutableArray class]])
{
[currentParent addObject:currentElementValue];
} else {
[currentParent setObject:currentElementValue forKey:currentElementName];
}
}
currentElementName=@"";
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentElementValue=string;
}
@end
That's it. I'm sure it's not the cleanest code (This is my very first XCode app) and there may be a better way of doing this, but it saved me from having to rewrite all my code that was retrieving data from SOAP services. I've seen tons of other people with the same problem and no solutions so far. I hope this helps.
|
Hello:
I followed your approach. I am able to call the web service. But I am having an issue please help me in that.
I have an application which when starts first time on the device, then it calls a web service which gives some data and stores it in a xml file for future use. After this call, I am reading the xml file and showing the data in table view. But for the first time, the file is not created before the view load, so no data is displayed. But if I run the application next time, as the file has been created , the table view displays the data.
My problem is that even in the first time also the data should be created before the view load and the data is displayed in the table view.
Please help me in this issue.
|
|
|
04-24-2009, 02:55 PM
|
#58 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 2
|
I noticed one person mentioned using RESTful web services instead of SOAP. This is my plan as well. My question is...are you still streaming XML or are you using something more lightweight like JSON?
Thanks,
Ci-Ci
|
|
|
04-29-2009, 12:05 AM
|
#59 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 2
|
Parser issue
Hello everyone
For my iphone application I need to request data about an object through a webservice.
I've used the code provided in this thread to do basic GET Request.
In fact, for now on, it just request an XML file located on a webserver ( http://.../lait.xml).
Here's the XML i'm supposed to parse :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<object>
<id>126</id> <!-- The object desserved has the id 126 in this kwys instance -->
<name>Milk</name> <!-- It is codenamed Milk -->
<information lang="fr"> <!-- We asked for french information -->
<title>Le lait</title> <!-- The title is the localized object's name -->
<!-- Here be the 3 types of content (for now): text, media, and link. -->
<!-- Each one may appear as many time as possible, in any order. -->
<text> <!-- A text entry is a caption followed by contents -->
<caption>Introduction</caption>
<!-- There must be at least one content Element. -->
<content>
Le lait est une boisson de couleur généralement blanchâtre
produite par
les mammifères femelles (y compris les monotrèmes).
Cette capacité
des femelles est une des caractéristiques définissant
les mammifères.
Le lait est produit par les cellules sécrétrices des
glandes
contenues dans les mamelles. Le lait sécrété dans les
premiers jours
après la parturition s'appelle le colostrum.
</content>
<content>
La fonction première du lait est de nourrir la progéniture
jusqu'à ce
qu'elle soit sevrée, c'est-à-dire capable de digérer
d'autres
aliments. Dans la plupart des civilisations humaines, le
lait des
animaux (eux-mêmes mammifères) domestiques (vache, brebis,
chèvre,
jument, dri, chamelle, dromadaire, bufflonne) est couramment
consommé.
</content>
<content>
Par analogie, on utilise également le terme de lait pour
désigner plusieurs boissons de consistance et/ou d'apparence
similaires et produites à base de végétaux, comme le lait de soja,
de coco, de riz, d'amande, d'avoine ou encore de pistache.
</content>
</text>
<media> <!-- Media refers to static media (picture) or dynamic/rich (video, ...). -->
<caption>Une fille pas douée en train de tout foutre à coté.</caption>
<href>http://upload.wikimedia.org/wikipedia/commons/5/59/MilkMaid.JPG</href>
</media>
<link> <!-- A link may refer to an external source, like this french wikipedia article. -->
<caption>Le lait sur wikipedia.</caption>
<href>http://fr.wikipedia.org/wiki/Lait</href>
</link>
<link> <!-- A link may also refer to another internal object, like this milk box. -->
<caption>La brique de lait.</caption>
<id>956</id>
</link>
</information>
</object>
The webrequest seems ok and here's the result from the NSDictionnary after going through server:
Code:
Response : {
object = {
id = 126;
information = {
link = {
caption = "La brique de lait.";
id = 956;
};
media = {
caption = "\U00e9e en train de tout foutre \U00e0 cot\U00e9.";
href = "http://upload.wikimedia.org/wikipedia/commons/5/59/MilkMaid.JPG";
};
text = {
caption = Introduction;
content = "\U00e9galement le terme de lait pour\n\t\t\t\td\U00e9signer plusieurs boissons de consistance et/ou d'apparence\n\t\t\t\tsimilaires et produites \U00e0 base de v\U00e9g\U00e9taux, comme le lait de soja,\n\t\t\t\tde coco, de riz, d'amande, d'avoine ou encore de pistache.\n\t\t\t";
};
title = "Le lait";
};
name = Milk;
};
}
The only thing is that the XMLParser seems to skip every node type he has already been going through (like <content> for example).
Could anyone tell me if there's a modification to do in the parser to correct that issue ?
I repost the parser .m file:
Code:
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
result=[[NSMutableDictionary alloc] init];
parentArray=[[NSMutableArray alloc] init];
[parentArray addObject:result];
currentElementName=@"";
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if (qName) {
elementName = qName;
}
currentElementValue=@"";
if (currentElementName!=@"")
{
id newParent=NULL;
if ([currentElementName isEqualToString:@"*Array*"])
{
newParent=[[NSMutableArray alloc] init];
} else {
newParent=[[NSMutableDictionary alloc] init];
}
[parentArray addObject:newParent];
}
currentElementName=elementName;
NSLog(@"Name %@", currentElementName); //to check the parser is really going through every node received from the web and it does. It just doesn't store 2 similar type of node.
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (qName) {
elementName = qName;
}
if (currentElementName==@"")
{
//We're adding a container with children. Add it to the parent and remove this item fromt he parentArray
int currentIndex=[parentArray count]-1;
int parentIndex=currentIndex - 1;
id currentChild=[parentArray objectAtIndex:currentIndex];
id currentParent=[parentArray objectAtIndex:parentIndex];
if ([currentParent isKindOfClass:[NSMutableArray class]])
{
[currentParent addObject:currentChild];
} else {
[currentParent setObject:currentChild forKey:elementName];
}
[parentArray removeObjectAtIndex:currentIndex];
} else {
//We're adding a simple type element
int currentIndex=[parentArray count]-1;
id currentParent=[parentArray objectAtIndex:currentIndex];
if ([currentParent isKindOfClass:[NSMutableArray class]])
{
[currentParent addObject:currentElementValue];
} else {
[currentParent setObject:currentElementValue forKey:currentElementName];
}
}
currentElementName=@"";
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentElementValue=string;
}
Hope someone can help. I just want a generic xml parser which gives me back a big NSDictionnary i can go through and get data.
Thanks
|
|
|
04-29-2009, 02:09 AM
|
#60 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 2
|
Mmmh after reading what i just wrote i suppose it's just the way i try to access the data that cause problems.
Could anyone tell me how i'm supposed to access the "caption" field of a link ?
I think i have to get a NSMutableArray from "information" with the key "link" then every row should be a NSDictionary and i should be able to query it by a simple objectForKey:@"caption" right ?
Last edited by Topper; 04-29-2009 at 05:20 AM.
|
|
|
04-30-2009, 01:37 PM
|
#61 (permalink)
|
|
New Member
Join Date: Apr 2009
Posts: 1
|
Secure it?
How is everyone securing their per-user access to the web service and managing a server-side session for that user?
i.e. assuming encryption is handled by SSL -- how do you set the user session context on the web server? By http authentication, or actually passing a username/password pair into a 'logon' operation on the web-service and a token afterwards? the userid/pw pair every operation call?
Any examples appreciated.
Last edited by nwhiteside; 04-30-2009 at 01:52 PM.
|
|
|
05-11-2009, 10:08 AM
|
#62 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 1
|
Does anyone know how to obtain the attributes of an xml tag invoquing a SOAP webservice?
I'm able to get the NSDictionary, but the attributes are missing.
Example:
XML returned by the service:
<son>
<child name="XYZ">
</son>
If I use the WSMethodInvocationInvoke method to invoque it, I will get the NSDictionary whith a "son" object but I wont get the "child" name attribute so it will be nil
but if the webservice returns
<son>
<child>XYZ</son>
</son>
I will get child the object with a value of XYZ
Any clue?
|
|
|
06-19-2009, 05:11 AM
|
#63 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 10
|
how to read a double value from the Webservice?
I am trying to read a double value returned by a webservice using NSXMLParser. In the event parser didEndElement, I am getting the correct value. But in the final output, the value gets reset to zero.
Could you please help me in this regard.....
Ramesh.
Quote:
Originally Posted by realberen
Thanks for the post. Just wondering, if you're interacting more, like sending data like you would normally do in the SOAP envelope, how do you do that in this scenario?
Cheers
Nik
|
|
|
|
06-27-2009, 06:51 PM
|
#64 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 87
|
Quote:
Originally Posted by varchar
As they say: "That's what discussion boards are for...."
Glad I can help....
|
Im looking to access a webservice to pull data into a tableview on my iphone app for the user to scroll through. Then when they find a title they like, they can select the title in the tableview and be taken to the image that is also being stored on the webserver.
Would you know where i can get some sample code for this?
this seems to be the thread for this stuff so any help would be greatly appreciated!
Thanks
Paul
Last edited by pmvinuelas; 06-27-2009 at 07:07 PM.
|
|
|
07-24-2009, 03:43 PM
|
#65 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 1
|
Sample
Can you send me the zip file for this code?
Thanks in advance!
Rob
robbonner@mac.com
|
|
|
08-13-2009, 12:51 PM
|
#66 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 5
|
Handling of an authentication key that somes back from a SOAP Login Method?
Hi, Thanks very much for posting these examples. These and the threads of replies have been very useful in getting a successful connection to a webservice I am trying to use. I did manage to call a Login method on my
service which sends back a session key. I assume that somehow I need to be sure to include this key in future requests to various methods (so it will know which session to use) What I am not sure is how to send the key. Does it get wrapped into a SOAP header? or does it somehow get associated with the NSURLRequest object? any thoughts, advice, examples appreciated.
|
|
|
08-14-2009, 07:13 AM
|
#67 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 1
|
Hi, i have one leak with xmlParser, and i dont know how fix it
Code:
XMLParser *xmlParser = [[XMLParser alloc] init];
// ... more code ...
return xmlParser.parentArray
And this is the method:
Code:
+(id)callRestService: (NSString *) url: (NSString *) methodName: (NSArray *) keys: (NSArray *) objects
{
NSURL *theURL=[WebServices generateWebServiceHTTPGetURL : url: methodName: keys: objects];
XmlParser *xmlParser = [[XmlParser alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
[parser setDelegate:xmlParser];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
return xmlParser.parentArray;
}
xmlParser not is released in the code, if i proof with autorelease but not work
Pls, can someone help me?
Thank you so much.
|
|
|
08-26-2009, 12:01 PM
|
#68 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 1
|
Quote:
Originally Posted by Trilitech
Fortunately asp.net already supports REST style requests (I didn't realize this before today), so no changes were needed on the web server side.
|
Well maybe one change...
This was not working for me. Then I realized it WAS working only from the same development machine that was running the service. You can see this feature in action on the test web service pages that ASP.NET builds automatically.
In order to make this feature of ASP.NET accessible to remote machines I had to add this to my web.config file in ASP.NET.
Code:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 468 |
| 45 members and 423 guests |
| axeman, beancreative, billk, black666, BrianSlick, Chonch, ChrisJW, ColouredRobot, dany88, DaveHolzer, dhouse, d_CFO, ggalante, GhostDog, harrytheshark, iPhoneAppStudio, iPhoneDevelopment, javaconvert, jorgmart, josep@sixtemia.com, Kalimba, KenPletzer, lukeca, maiuridavide, marco.mussini75, Maximilian, mebarron, MiniRobinho, mquetel, msu, ng93, nibby, NicolasD, Noise, P2k, PeddlePower, raees, rarindeed, Sai Baba, shibq, Slum, svguerin3, tralhavet, ziconic, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,502
Threads: 35,781
Posts: 156,768
Top Poster: smasher (2,449)
|
| Welcome to our newest member, msu |
|