use*_*602 9 json web-services objective-c ios
我正在尝试使用目标c中的参数调用一个简单的JSON Web服务.到目前为止不起作用.
这是Web服务方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void LogIn(string username, string password)
{
Context.Response.Write(username + "___" + password);
Context.Response.End();
}
Run Code Online (Sandbox Code Playgroud)
这是我的Objective C代码:
// Build dictionnary with parameters
NSString *username = @"usernameTest";
NSString *password = @"passwordTest";
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:username forKey:@"username"];
[dictionnary setObject:password forKey:@"password"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
options:kNilOptions
error:&error];
NSString *urlString = @"http://localhost:8080/ListrWS.asmx/LogIn";
NSURL *url = [NSURL URLWithString:urlString];
// Prepare the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonData];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if (errorReturned)
{
//...handle the error
}
else
{
NSString *retVal = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@", retVal);
}
Run Code Online (Sandbox Code Playgroud)
这是什么:
NSLog(@"%@", retVal);
Run Code Online (Sandbox Code Playgroud)
显示:
{"Message":"Thread was being aborted.","StackTrace":" at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\r\n
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)\r\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)\r\n
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Threading.ThreadAbortException"}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?
我有类似的问题。我像您一样使用 jsonData 设置 HTML 主体,但它不起作用。结果发现 JSON 服务没有按预期配置。
因此,不要设置 HTML 正文,而是尝试像 GET 方法一样调用 URL。
我的意思是,删除设置 HTML 正文的行,并将 URL 更改为
http://localhost:8080/ListrWS.asmx/LogIn?username=usernameTest&password=passwordTest
Run Code Online (Sandbox Code Playgroud)
不要更改方法 (POST)。
如果这有效,您将必须在服务器端做一些工作。
归档时间: |
|
查看次数: |
15020 次 |
最近记录: |