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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With