先下載待會要用到的工具 WSDL2ObjC-0.6.zip WSDL2ObjC-0.7-PRe1.zip
創建WebService服務項目后先在Web.config添加節點,設置WebService遠程調試訪問,否則會出現:
“測試窗體只能用于來自本地計算機的請求”。
<webServices><protocols><add name="HttpSoap" /><add name="HttpPost" /><add name="HttpGet" /><add name="Documentation" /></protocols></webServices>
創建類庫添加Model實體類
public class CourseEntity{ private int courseID; public int CourseID { get { return courseID; } set { courseID = value; } } private int parentID; public int ParentID { get { return parentID; } set { parentID = value; } } private string courseName; public string CourseName { get { return courseName; } set { courseName = value; } } private string coursePPT; public string CoursePPT { get { return coursePPT; } set { coursePPT = value; } } private string courseVidio; public string CourseVidio { get { return courseVidio; } set { courseVidio = value; } } }
事先做好Model實體類的生成dll文件,直接添加引用至bin目錄下
接下來我們打開Service.cs文件。
service.cs代碼如下:
[WebMethod(Description = "ProblemPaper")] public List<ProblemPaperEntity> ProblemPaper(String prkid) { return dbOperation.ProblemPaper(prkid); }
DBOperation.cs代碼如下:
/// <summary> /// 題庫試卷目錄表 ProblemPaper /// </summary> /// <returns>PPID(編號)PRKID(上一級)PTID(類型編號)Name(名稱)ProblemNum(目錄數量))</returns> public List<ProblemPaperEntity> ProblemPaper(String prkid) { List<ProblemPaperEntity> list = new List<ProblemPaperEntity>(); ProblemPaperEntity model = null; try { string sql = "select PPID,PRKID,PTID,Name,ProblemNum,Row_Number() over(order by PPID ) as row_number from ProblemPaper where 1=1"; sql += " and prkid in ( select * from getProblemResourseByID("+prkid+"))"; string s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(s); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { model = new ProblemPaperEntity(); model.PPID = reader.GetInt32(0); model.PRKID = reader.GetInt32(1); model.PTID = reader.GetInt32(2); model.Name = reader.GetString(3); model.ProblemNum = reader.GetInt32(4); list.Add(model); } reader.Close(); cmd.Dispose(); } catch (Exception) { } return list; }
從service.cs代碼中可以看到ProblemPaper方法前面 [WebMethod]指示web服務提供的方法
public方法能否被調用者訪問取決于這個方法是否是一個“WebMethod”,在編寫方法的時候注意
方法前面是否含有WebMethod
service.cs 代碼如下:
[WebMethod(Description = "JsonProblemPaper2")] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string JsonProblemPaper2(String prkid) { return new javaScriptSerializer().Serialize(dbOperation.ProblemPaper(prkid)); }
DBOperation.cs代碼如下:
public List<string> JsonProblemPaper(String prkid) { List<String> list = new List<String>(); try { string sql = "select PPID,PRKID,PTID,Name,ProblemNum,Row_Number() over(order by PPID ) as row_number from ProblemPaper where 1=1"; sql += " and prkid in ( select * from getProblemResourseByID(" + prkid + "))"; string s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(s); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { list.Add(reader[0].ToString()); list.Add(reader[1].ToString()); list.Add(reader[2].ToString()) ; list.Add(reader[3].ToString()); list.Add(reader[4].ToString()); } reader.Close(); cmd.Dispose(); } catch (Exception) { } return list; }
返回Json格式,需要在方法前面同時聲明
[WebMethod(Description = "××××")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
返回格式: return new JavascriptSerializer().Serialize(dbOperation.ProblemPaper(prkid));
接下來的方法編寫由自己擴展,編寫完WebService
3.部署
環境windows Server2008 IIS 7.5 SqlServer2012
選擇應用程序池.Net v.2.0,切記,如在部署遇到問題可以查閱其他相關資料,在這里
就不多詳細
4.IOS基于wsdl2objc調用asp.net WebService
4.1使用wsdl2objc工具
在官網上下載有2個版本,我用的是WSDL2ObjC-0.6.zip,
部署完畢后,打開wsdl2objc
Parse WSDL后稍等15秒左右出現Finish!查看導入目錄
將生成的所有文件放置在wsdl2objc文件夾
嘗試編譯出現錯誤如下:
解決方法:這里有2個錯誤
1."libxml/tree.h" file not found
2.ARC開啟與禁止
第一個錯誤解決方法如下:
支持libxml2
TARGETS -> Build Settings -> Linking -> Other Linker Flags,設置“-lxml2”
TARGETS -> Build Settings -> Search Paths-> Header Search Paths,設置“/usr/include/libxml2”
TARGETS -> Build Settings -> Apple LLVM5.0-Language-Objective C> Objective-C Automatic Reference Counting,設置“No”
第二個錯誤解決方法:
TARGETS -> Build Settings ->All 搜索"compiler"
Apple LLVM 5.0- Custom CompilerFlags OtherWaning Flags 設置"-Wall"
打開Xcode的自動轉換工具
錯誤解決后,項目就可以完整的運行了,在第一個錯誤當中我花的時間有些多,
如果你在做的過程當中遇到這個錯誤
請重新嘗試再做看上面二個錯誤的解決方法,在第二個錯誤記得一次性轉換ARC
在項目當中還會用到手動設置ARC
手動ARC設置方法如下:
1.在Compiler Flags一列加上-fno-objc-arc就表示禁止這個.m文件的ARC
2.在Compiler Flags一列加上-fobjc-arc就表示開啟這個.m文件的ARC
參考資料:http://blog.csdn.net/a283127993/article/details/11082179
http://blog.csdn.net/q199109106q/article/details/8565403
5. IOS客戶端解析xml,json數據
接下來詳細說明如何解析xml,json,往往遇到問題我就花了就是整整一天時間來做
5.1 IOS客戶端解析xml無參數據
代碼如下:
-(void)getXml{ NSMutableArray *result;ServiceSoap12Bingding *binding =[Service ServiceSoap];Service_Course *request = [[Service_Course alloc] init];ServiceSoap12BindingResponse *response = [binding CourseUsingParameters:request];for(id mine in response.bodyParts){ if([mine isKindOfClass:[Service_CourseResponse class]]){ [request release]; result = [mine CourseResult].CourseEntity;}for(Service_CourseEntity *t in result){ NSLog(@"ID:%d ParentID:%d CourseName:%@ CoursePPT:%@ CourseVidio:%@",[t.CourseID intvalue],[t.ParentID intvalue],t.CourseName,t.CoursePPT,t.CourseVidio);}} }
-(void)getXml2{ NSMutableArray *result;ServiceSoap12Bingding *binding =[Service ServiceSoap];Service_ProblemPaper *request = [[Service_ProblemPaper alloc] init];request.prkid=@"1";ServiceSoap12BindingResponse *response = [binding ProblemPaperUsingParameters:request];for(id mine in response.bodyParts){ if([mine isKindOfClass:[Service_ProblemPaperResponse class]]){ [request release]; result = [mine ProblemPaperResult].ProblemPaperEntity;}for(Service_CourseEntity *t in result){ NSLog(@"PPID:%d],[t.PPID intvalue]);}}
代碼如下:
代碼如下:
-(void)getJson{ NSMutableArray *result; NSData*data;ServiceSoap12Bingding *binding =[Service ServiceSoap];Service_JsonProblemPaper2 *request = [[Service_JsonProblemPaper2 alloc] init];request.prkid=@"1";ServiceSoap12BindingResponse *response = [binding JsonProblemPaper2UsingParameters:request];for(id mine in response.bodyParts){ if([mine isKindOfClass:[Service_JsonProblemPaper2Response class]]){ [request release]; result = [mine JsonProblemPaper2Result] ; data= [result dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"data:%@",data); NSDictionary *dict =[NSjSONSerialization JSONbjectWithData:data options:NSJSONReadingAllowFragmentS error:nil];if(dict == nil){ return ;}else{ for(NSString *ds in dict) { NSLog(@"json%@",[ds objectForKey:@"Name"]); }}}}
在這里我只對解析json有參數據說明,在這里我遇到不少問題,花的時間挺多的,
IOS客戶端解析xml有參數數據,IOS客戶端解析xml有參數數據
參考代碼就可以實現,在解析json有參數據,遇到了幾個問題,
就幾行代碼也花了好久,斷斷續續抽出時間做,最后才完成,下面是如何將NSString
最后完整的放入NSDictionary,并且取出相應的鍵值,result是返回類型的數據
[result dataUsingEncoding:NSUTF8StringEncoding];
將result類型的數據,轉成UTF8的數據
首先我們將result類型的數據,轉成UTF8的數據
將JSON串轉化為字典
開始的時候想將返回的NSString數據轉化為NSDictionary即NSString-NSDictionary返回的數據為null
所以采用NSString-NSData-NSDictionary最后成功解決數據為null問題,數據成功拿到Name屬性值和其他屬性值
在這里我只打印Name屬性值
6.總結
該博文面向初學者,大牛請不要噴。寫到這里,又復習了好多知識,遇到之前沒發現的錯誤,但是耐心下來,問題總會解決,
WebService和客戶端源碼有需要的話可以留下郵箱,既然來了,對你有幫助,推薦支持一下唄!
http://www.49028c.com/linmingjun/p/4382565.html 作者
新聞熱點
疑難解答