iOS Development--Cloud development (cloud request &&xml resolution)

Source: Internet
Author: User
Tags uikit

Overview

This chapter provides a brief look at how cloud requests are made in iOS and what data is returned in the cloud, including Nsurl, and the XML parsing section. The specific use is a request application that queries the IP.

results show


(It took 10 times to use it)

Process Overview

1. Select the cloud Request link for the query IP and try the effect in the browser to find out the format of the query return result.

2. Create a new project, drag a Uitextfield and UIView, respectively, as input IP address information and query results display

3. When making a cloud request, construct the query's address link based on the IP of the query, then use Nsurl to generate the link, construct the request using Nsurlrequest, and finally use Nsurlconnection to make the request

4. Since Nsurlconnection is an asynchronous request, processing its return result requires the use of its proxy, first specifying its proxy, and then implementing the receive data method within its proxy (Connection: Didreceivedata) and the method of receiving data completion (connectiondidfinishloading)

5. Use Nsxmlparser to parse the returned results (with the need to use their proxies) in receiving data completion, because the class is parsed using sax, so it's annoying to use the feeling

6. The query format obtained is as follows:

<arrayofstring xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/ XmlSchema "xmlns=" http://WebXml.com.cn/"><string>112.134.113.124</string><string> Huazhong University of Science and Technology </string></ArrayOfString>

Main codeh file

  viewcontroller.h//  ipinfo////  Created by God Lin on 14/12/11.//  Copyright (c) 2014 Arbboter. All rights reserved.//#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller{    iboutlet uitextfield* _textip;    Iboutlet uitextview* _textresult;        nsstring* _stringrecivedata;    nsmutabledictionary* _dictip;    nsmutablearray* _arrayip;    nsstring* _currentcontext;} @property (nonatomic, retain) uitextfield* _textip; @property (nonatomic, retain) uitextview* _textresult; @property ( Nonatomic, retain) nsstring* _stringrecivedata, @property (nonatomic, retain) nsmutabledictionary* _dictip; @property ( Nonatomic, retain) nsmutablearray* _arrayip, @property (nonatomic, retain) nsstring* _currentcontext;-(ibaction) go;@ End

m file

viewcontroller.m//ipinfo////Created by God Lin on 14/12/11.//Copyright (c) 2014 Arbboter. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation Viewcontroller@synthesize _textip, @synthesize _textresult, @synthesize _stringrecivedata; @synthesize _arrayip;@    synthesize _dictip; @synthesize _currentcontext;-(ibaction) go{_stringrecivedata = @ "";        [Self._textip Resignfirstresponder]; Request connection nsurl* URL = [nsurl urlwithstring:[nsstring stringwithformat:@ "http://webservice.webxml.com.cn/WebServices/    ipaddresssearchwebservice.asmx/getcountrycitybyip?theipaddress=%@ ", Self._textip.text]];    nsurlrequest* request = [Nsurlrequest Requestwithurl:url]; [Nsurlconnection connectionwithrequest:request delegate:self];} #pragma nsurlconnectiondelegate protocol//Receive data call this method, may not receive all data at once, so: -(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{nsstring* str = [[NSString alloc] In Itwithdata:data Encoding:nsutf8strIngencoding]; if ([self._stringrecivedata length]) {self._stringrecivedata = [Self._stringrecivedata stringbyappendingstring:s    TR];    } else {self._stringrecivedata = str; } NSLog (@ "%@", str);} Data receive completion call the method-(void) connectiondidfinishloading: (nsurlconnection *) connection{nsxmlparser* xmlparser = [[        Nsxmlparser alloc] Initwithdata:[self._stringrecivedata datausingencoding:nsutf8stringencoding]];    Xmlparser.delegate = self; [Xmlparser parse];} #pragma nsxmlparser protocol//Start parsing XML Call this method-(void) Parserdidstartdocument: (Nsxmlparser *) parser{self._dictip = [[ Nsmutabledictionary alloc] init];}    The XML parsing end calls the method-(void) Parserdidenddocument: (Nsxmlparser *) parse{nsstring* info = nil;    static int n = 0; For (ID IP in self._dictip) {info = [NSString stringWithFormat: @ "%-5d [%@,%@]\n", ++n, IP, [se                Lf._dictip Objectforkey:ip]];    Self._textresult.text = [Self._textresult.text stringbyappendingstring:info];} [Self._dictip removeallobjects]; Self._textip.text = @ "";} Parsing XML encounters a start tag called the Method-(void) Parser: (Nsxmlparser *) Parserdidstartelement: (NSString *) elementname NamespaceURI: ( NSString *) NamespaceURI qualifiedname: (NSString *) QualifiedName attributes: (nsdictionary *) attributedict{if ([Eleme    Ntname isequaltostring:@ "arrayofstring"]) {Self._arrayip = [[Nsmutablearray alloc] init]; } Self._currentcontext = @ "";} Parsing XML encounters an end tag called the Method-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (nsstring *) NamespaceURI QualifiedName: (NSString *) qname{if ([self._currentcontext length] > 0) {[Self._arrayip add            Object:self._currentcontext];    } if ([Self._arrayip count] = = 2) {[Self._dictip setobject:self._arrayip[1] forkey:self._arrayip[0]]; }}//parsing XML calls this method when it encounters the label content//(there is a possibility that a label content will not trigger the function more than once)-(void) Parser: (Nsxmlparser *) Parserfoundcharacters: (NSString *) string {//Remove spaces and newline string = [string stringbYtrimmingcharactersinset: [Nscharacterset Whitespacecharacterset]];    string = [string stringbyreplacingoccurrencesofstring:@ "\ n" withstring:@ ""];    if ([string length] > 0) {self._currentcontext = [self._currentcontext stringbyappendingstring:string];    }}-(void) viewdidload{[Super Viewdidload];    Additional setup after loading the view, typically from a nib.    self._textresult.editable = NO;    Self._textresult.backgroundcolor = [Uicolor colorwithpatternimage:[uiimage imagenamed:@ "Bg.jpg"]; Self._textresult.textcolor = [Uicolor yellowcolor];}    -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

Engineering Code

iOS Development--Cloud development (cloud request &&xml resolution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.