使用xcode 5和ios 7中的WCF服务
我已经创建了一个WCF服务来在sql server中插入数据,因此我遵循了下面的结构 我首先创建了方法和他的实现 在WCF界面中:使用xcode 5和ios 7中的WCF服务,wcf,ios7,xcode5,Wcf,Ios7,Xcode5,我已经创建了一个WCF服务来在sql server中插入数据,因此我遵循了下面的结构 我首先创建了方法和他的实现 在WCF界面中: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/InsertEmployee/{id1}/{id2}/{id3}")]
bool InsertEmployeeMethod(string id1,string id2, string id3);
在执行中:
public bool InsertEmployeeMethod(string id1, string id2, string id3)
{
int success = 0;
using (SqlConnection conn = new SqlConnection("Data Source=NXT1;User ID=zakaria;Password=11;Initial Catalog=EmpDB;Integrated Security=false"))
{
conn.Open();
decimal value = Decimal.Parse(id3);
string cmdStr = string.Format("INSERT INTO EmpInfo VALUES('{0}','{1}',{2})", id1, id2, value);
SqlCommand cmd = new SqlCommand(cmdStr, conn);
success = cmd.ExecuteNonQuery();
conn.Close();
}
return (success != 0 ? true : false);
}
要测试web服务,请尝试该URL:
"41.142.251.142/JsonWcfService/GetEmployees.svc/json/InsertEmployee/myName/MylastName/6565"
在这里一切正常之前
为了测试这个web服务,我在Main.storyboard中创建了3个文本字段(firstName.text、lastName.text和salary.text,以及一个发送输入数据的按钮)。请注意,我使用过xcode 5和ios 7
我在viewcontroller.m中声明了url,如下所示:
myViewController.m:
#define BaseWcfUrl [NSURL URLWithString:@"41.142.251.142/JsonWcfService/GetEmployees.svc/json/InsertEmployee/{id1}/{id2}/{id3}"]
然后我实现了与click按钮相关的Insert Employee方法
-(void) insertEmployeeMethod
{
if(firstname.text.length && lastname.text.length && salary.text.length)
{
NSString *getValue= [BaseWcfUrl stringByAppendingFormat:@"InsertEmployee/%@/%@/%@",firstname.text,lastname.text,salary.text];
NSURL *WcfServiceURL = [NSURL URLWithString:getValue];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:WcfServiceURL];
[request setHTTPMethod:@"POST"];
// connect to the web
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *respStr = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:respData
options:NSJSONReadingMutableContainers
error:&error];
NSNumber *isSuccessNumber = (NSNumber*)[json objectForKey:@"InsertEmployeeMethodResult"];
}
}
所以我遇到的问题是系统总是将str值返回为“Nil”
因此,您可以在此处看到所有错误消息:
2014-02-17 19:49:49.419 InsertData[2384:70b]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“数据参数为零”
*第一次抛出调用堆栈:
0 CoreFoundation 0x017395e4例外预处理+180
1 libobjc.A.dylib 0x014bc8b6 objc_异常_抛出+44
2 CoreFoundation 0x017393bb+[N异常提升:格式:][139
3基金会0x0120 37 A2+ [ NSJSONSORM归一化JSONObjuts:数据:选项:错误:] + 67</P> 4 InsertData 0x0000236c-[ViewController sendData:+1452
5 libobjc.A.dylib 0x014ce874-[NSObject性能选择器:withObject:withObject:+77 6 UIKit 0x0022C2-[UIApplication sendAction:to:from:forEvent:][108 7 UIKit 0x0022c04e-[UIApplication sendAction:toTarget:fromSender:forEvent:+61 8 UIKit 0x003240c1-[UIControl发送操作:发送到:forEvent:+66 9 UIKit 0x00324484-[UIControl\u发送操作预防:带事件:][577 10 UIKit 0x00323733-[UIControl触摸已结束:带事件:][641 11 UIKit 0x0026951d-[UIWindow\u sendTouchesForEvent:+852 12 UIKit 0x0026a184-[UIWindow sendEvent:+1232 13 UIKit 0x0023de86-[UIApplication sendEvent:+242 14 UIKit 0x0022818f _UIApplicationHandleEventQueue+11421 CoreFoundation 0x016c283fCFRUNLOOP\u正在调用\u OUT\u到\u A\u SOURCE0\u PERFORM\u函数+15 16 CoreFoundation 0x016c21cb _CFRunLoopDoSources0+235 17 CoreFoundation 0x016df29e_uuCFRUnloopRun+910 18 CoreFoundation 0x016deac3 CFRunLoopRunSpecific+467 19 CoreFoundation 0x016de8db CFRUNLOOPSRUNINMODE+123 20图形服务0x036de9e2 GSEventRunModal+192 21图形服务0x036de809 GSEventRun+104 22 UIKit 0x002AD3B UIApplicationMain+1225 23插入数据0x00002d5d主+141 24 libdyld.dylib 0x01d7770d启动+1 ) libc++abi.dylib:以NSException类型的未捕获异常终止 你能帮我一下吗,我的压力很大非常感谢。我想你的URL搞错了,你添加了两次
InsertEmployee
将BaseWcfUrl
更改为[nsurlwithstring:@“41.142.251.142/JsonWcfService/GetEmployees.svc/json/”]
而不是[nsurlwithstring:@“41.142.251.142/JsonWcfService/GetEmployees.svc/json/InsertEmployee/{id1}/{id2}/{id3}”
要绝对确定,请将最终URL记录到控制台(或在调试器中查看),以确保其正确
另外,在代码中,在
NSURL*WcfServiceURL=[NSURL-URLWithString:str]中代码>,str
来自哪里?你是说getValue
那里吗?你好,谢谢你的回复,我已经把str编辑成getValue了。我会按照你的建议做,然后返回给你我将BaseWcfUrl更改为URL“[NSURL URLWithString:@”41.142.251.142/JsonWcfService/GetEmployees.svc/json/”]”我将BaseWcfUrl更改为URL“[NSURL URLWithString:@”41.142.251.142/JsonWcfService/GetEmployees.svc/json/”]”,但现在系统出现另一个错误;[NSURL stringByAppendingFormat::发送到实例0x8a630b0的选择器无法识别。由于未捕获的异常“NSInvalidArgumentException”、“原因:”-[NSURL stringByAppendingFormat::发送到实例0x8a630b0的选择器无法识别,正在终止应用程序
NSString *getValue= [BaseWcfUrl stringByAppendingFormat:@"InsertEmployee/%@/%@/%@",firstname.text,lastname.text,salary.text];