2012-04-01 17 views
9

Sto provando a chiamare un semplice servizio web JSON con un parametro nell'obiettivo c. Non funziona finora.Chiamata al servizio Web JSON con parametri - Objective C - iOS

Ecco il metodo di servizio Web:

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

Ecco il mio codice C Obiettivo:

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

} 

Ecco cosa:

NSLog(@"%@", retVal); 

visualizzazione:

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

Qualche idea?

+0

Ciò che viene registrato sul lato server? – Perception

risposta

2

Ho avuto un problema simile. Stavo impostando il corpo HTML con jsonData come si fa e non ha funzionato. Si è scoperto che il servizio JSON non era configurato come avrebbe dovuto essere.

Quindi, anziché impostare il corpo HTML, provare a chiamare l'URL come metodo GET.

Voglio dire, rimuovere le righe che si imposta il corpo HTML e modificare l'URL per

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

Non modificare il metodo (POST).

Se funziona, è necessario lavorare sul lato server.

0

Per la parte iOS,

Il codice sembra OK. Non dovrebbero esserci problemi sul lato client. Ma puoi fare i seguenti debuggings;

  • Poco prima di chiamare sendSynchronousRequest:returningResponse:error inserire un punto di interruzione e controllare se jsonData è valido. Perché non stai controllando lo error che hai assegnato per la serializzazione JSON.
  • Se non ci sono problemi con i dati JSON, trovare un altro server JSON di base e provare a consumarlo. Se il tuo lato client funziona, saprai che c'è qualcosa di sbagliato sul tuo lato server.

Acclamazioni

Problemi correlati