使用参数调用JSON Web服务 - Objective C - iOS [英] Calling JSON web service with parameters - Objective C - iOS

查看:134
本文介绍了使用参数调用JSON Web服务 - Objective C - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用目标c中的参数调用简单的JSON Web服务。到目前为止无效。



以下是网络服务方式:

  [WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void LogIn(string username,string password)
{
Context.Response.Write(username +___+密码);
Context.Response.End();
}

这是我的目标C代码:

  //使用参数构建字典
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];

//准备请求
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[请求setHTTPMethod:@POST];
[请求setValue:@application / jsonforHTTPHeaderField:@Accept];
[请求setValue:@application / jsonforHTTPHeaderField:@Content-Type];
[请求setValue:@jsonforHTTPHeaderField:@Data-Type];
[请求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错误
}
else
{
NSString * retVal = [[ NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@%@,retVal);

}

以下是:

  NSLog(@%@,retVal); 

显示:

  {Message:Thread is aborted。,StackTrace:at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo方法,Object target,Object []参数,SignatureStruct& sig,MethodAttributes methodAttributes,RuntimeType typeOwner System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化,布尔值skipVisibilityChecks)\\ nn 

在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)\\\\ n
at System.Reflection.MethodBase.Invoke(Object obj,Object [参数)\\\ at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target,IDictionary`2 parameters)\ r \ n at
System.Web.Script.Services.RestHandler .InvokeMethod(HttpContext context,WebServiceMethodData System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)中的methodData,IDictionary`2 rawParams)\\\\ n,ExceptionType:System.Threading.ThreadAbortException}

任何想法?

解决方案

我遇到了类似的问题。我正在使用jsonData设置HTML正文,但它不起作用。事实证明,JSON服务没有按原样配置。



因此,不要设置HTML正文,而是尝试像GET方法一样调用URL 。



我的意思是,删除您设置HTML正文的行,并将URL更改为

  http:// localhost:8080 / ListrWS.asmx / LogIn?username = usernameTest& password = passwordTest 

不要改变方法(POST)。



如果这样做,你将不得不在服务器端做一些工作。 / p>

I'm trying to call a simple JSON webservice with a parameter in objective c. Doesn't work so far.

Here is the web service method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void LogIn(string username, string password)
{
    Context.Response.Write(username + "___" + password);
    Context.Response.End();
}

Here is my Objective C code:

// 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);

}

Here is what:

NSLog(@"%@", retVal);

display:

{"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"}

Any ideas ?

解决方案

I had a similar problem. I was setting the HTML body with jsonData as you do and it didn't work. It turned out that the JSON service wasn't configured as it was supposed to be.

So, instead of setting the HTML body, try calling the URL like a GET method.

I mean, remove the lines that you set the HTML body, and change the URL to

http://localhost:8080/ListrWS.asmx/LogIn?username=usernameTest&password=passwordTest

Don't change the method (POST).

If this works, you will have to do some work on the server side.

这篇关于使用参数调用JSON Web服务 - Objective C - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆