2011-03-22 15 views
7

Estoy tratando de enviar la información al servidor y obtener la respuesta. Los datos están llegando al servidor, pero no recibo ninguna respuesta. El valor de los datos de respuesta es nula BCD de la que está lanzando una excepción,Iphone Http request response using json

-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x4e2dd70 {NSLocalizedDescription=Unexpected end of string}" 

Puede alguien ayudarme pls ....

Mi código:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.83:8082/WebServiceProject/AcessWebservice?operation=login"]]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSError *theError = NULL; 
NSArray *keys = [NSArray arrayWithObjects:@"UserId", @"Password", nil]; 
NSArray *objects = [NSArray arrayWithObjects:@"rajin.sasi", @"abhi1551", nil]; 
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 


NSString* jsonString = [jsonDictionary JSONRepresentation]; 

SBJSON *jsonParser = [SBJSON new]; 
[jsonParser objectWithString:jsonString]; 

NSLog(@"Val of json parse obj is %@",jsonString); 
[request setHTTPMethod:@"POST"]; 

[request setValue:jsonString forHTTPHeaderField:@"json"]; 
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];  
[request setHTTPBody:responseData]; 

NSMutableString* stringData= [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSDictionary *jsonDictionaryResponse = [stringData JSONValue]; 

NSString *json_message=[jsonDictionaryResponse objectForKey:@"message"]; 

printf("Json string is %s **********",[json_message UTF8String]); 

Respuesta

12

estoy no está al tanto de los detalles de su servicio web, pero el código a continuación podría ser el origen de su problema (¡o al menos uno de ellos!)

[request setValue:jsonString forHTTPHeaderField:@"json"]; 
responseData = [NSURLConnection sendSynchronousRequest:request  returningResponse:&theResponse error:&theError];  
[request setHTTPBody:responseData]; 

Usted está enviando la solicitud antes de configurar el cuerpo, que supongo que debe incluir su contenido jsonString. Además, está asignando su jsonString a un campo de encabezado, ¿está seguro de que eso es lo que quiere? Aquí hay una pista sobre lo que podría funcionar:

[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
[request setHTTPBody:jsonString]; 
responseData = // rest of your code here.... 

le sugiero que eche un vistazo a través de ese código, ya que es un desastre en el momento! Tiene dos NSURLConnection solicitudes de ir allí, uno asynchronous y uno synchronous, que es un poco difícil de entender qué/por qué está haciendo todo esto a fin de comprobar la documentación de Apple para NSURLConnection y ordenar su código ...

[EDITAR ]

aquí está mi sugerencia para usted:

NSError *theError = nil; 
NSArray *keys = [NSArray arrayWithObjects:@"UserId", @"Password", nil]; 
NSArray *objects = [NSArray arrayWithObjects:@"rajin.sasi", @"abhi1551", nil]; 
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
NSString *jsonString = [jsonDictionary JSONRepresentation]; 
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.83:8082/WebServiceProject/AcessWebservice?operation=login"]]; 
[request setValue:jsonString forHTTPHeaderField:@"json"]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:jsonData]; 
NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];  
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSDictionary *jsonDictionaryResponse = [string JSONValue]; 
[string release]; 
[theResponse release]; 
+1

Gracias por la respuesta Rog, pero el código para la solicitud está funcionando bien, el problema es con la respuesta ... Los datos de la respuesta no tienen ningún valor en respuesta ... – Akshay

+0

Ok, entonces si crees que está bien, entonces yo supongo que está bien! – Rog

+0

¿hay algún error o error en la respuesta ... responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & theResponse error: & theError]; \t \t \t \t \t \t \t NSMutableString * StringData = [[NSString alloc] initWithData: ResponseData codificación: NSUTF8StringEncoding]; \t NSDictionary \t * jsonDictionaryResponse = [stringData JSONValue]; \t \t \t NSString * json_message = [jsonDictionaryResponse objectForKey: @ "message"]; \t printf ("La cadena Json es% s **********", [json_message UTF8String]); – Akshay

0

Prueba esto:

[request setValue:jsonString forHTTPHeaderField:@"json"]; 
responseData = [NSURLConnection sendSynchronousRequest:request  returningResponse:&theResponse error:&theError];  
[request setHTTPBody:responseData]; 
2
NSData* responseData = nil; 

    NSURL *url=[NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    responseData = [NSMutableData data] ; 

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 


    NSString *bodydata=[NSString stringWithFormat:@"%@",jsonString]; 

    NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]]; 

    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:req]; 
    [request setTimeoutInterval:15.0]; 

    NSURLResponse* response; 
    NSError* error = nil; 

    responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    NSError *dataError; 

    NSMutableDictionary * jsonDict = [[NSMutableDictionary alloc]init]; 

    if (responseData != nil) 
    { 
     jsonDict = [NSJSONSerialization JSONObjectWithData:responseData 
                options:kNilOptions 
                error:&dataError]; 
      NSLog(@"jsonDict:%@",jsonDict); 

    }