2012-04-01 10 views
9

Estoy tratando de llamar a un servicio web JSON simple con un parámetro en el objetivo c. No funciona hasta ahora.Llamando al servicio web JSON con parámetros - Objetivo C - iOS

Aquí es el método de servicio Web:

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

Aquí es mi Objetivo código 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); 

} 

Esto es lo que:

NSLog(@"%@", retVal); 

pantalla:

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

¿Alguna idea?

+0

Lo que se registra en el lado del servidor? – Perception

Respuesta

2

Tuve un problema similar. Estaba configurando el cuerpo HTML con jsonData como lo hace y no funcionó. Resultó que el servicio JSON no estaba configurado como se suponía que era.

Por lo tanto, en lugar de configurar el cuerpo HTML, intente llamar a la URL como un método GET.

Es decir, eliminar las líneas que se configuran el cuerpo HTML, y cambiar el URL para

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

No cambie el método (POST).

Si esto funciona, tendrá que trabajar en el lado del servidor.

0

Para la pieza de iOS,

Su código se ve bien. No debería haber ningún problema en el lado del cliente. Pero puede hacer las siguientes depuraciones;

  • Justo antes de llamar al sendSynchronousRequest:returningResponse:error inserte un punto de interrupción y verifique si su jsonData es válido. Porque no está marcando el error que asignó para la serialización JSON.
  • Si no hay ningún problema con sus datos JSON, busque otro servidor JSON básico e intente consumirlo. Si su lado del cliente funciona, sabrá que hay algo mal en su lado del servidor.

Saludos

Cuestiones relacionadas