Hi
I have one class to download information..... The information download correctly, but why the connectionDidFinishLoading method doesn't trigger?
My "h file" code
The "m file" code, first execute "connectToWSArticulo" method, and then "descargar" method and any trigger the "connectionDidFinishLoading" method . The informartion it's download correct.
The code to implement the class..
I have one class to download information..... The information download correctly, but why the connectionDidFinishLoading method doesn't trigger?
My "h file" code
Code:
#import <Foundation/Foundation.h>
#import "Reachability.h"
//implementar protocolo
@protocol RedDelegate <NSObject>
-(void) terminaDescarga:(NSData*)datos
conId:(NSInteger)id
httpCodeReturn:(int)code
httpStringCodereturn:(NSString*)codestring;
-(void) errorEnDescarga:(NSInteger)codigo descrip:(NSString *)descError conId:(NSInteger)id;
@end
@interface JFRed : NSObject <NSURLConnectionDataDelegate> @property(nonatomic,strong) NSObject<RedDelegate> *delegado;
@property (nonatomic,strong) NSMutableData *datosDescargados;
@property (nonatomic,strong) NSURLConnection @property(nonatomic) NSInteger id;
@property(nonatomic) int httpCodeReturn;
@property(nonatomic) NSString *httpStringCodereturn;
-(void) descargar:(NSString*)direccion conId:(NSInteger)id;
-(void) jsonPostRequest:(NSString *)direccion tipoId:(NSInteger)id postData:(NSString*) postData;
-(void) comprobarRed:(NSString*)direccion;
-(NSDictionary*) connectToWSArticulo:(NSString *) lstrIdArticulo;
@end
The "m file" code, first execute "connectToWSArticulo" method, and then "descargar" method and any trigger the "connectionDidFinishLoading" method . The informartion it's download correct.
Code:
#import "JFRed.h"
@implementation JFRed
-(void) descargar:(NSString*)direccion conId:(NSInteger)id
{
self.id = id; //es el número de tarea que se esta bajando
NSString *dir = direccion;
NSURL *url = [NSURL URLWithString:dir];
NSMutableURLRequest *solicitud = [NSURLRequest requestWithURL:url];
//inicia la descarga asincrona
self.conexion = [[NSURLConnection alloc] initWithRequest:solicitud delegate:self];
if(self.conexion!=nil)
{
self.datosDescargados = [NSMutableData data];//inicializa
}
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse* httpReturn = (NSHTTPURLResponse*)response;
self.httpCodeReturn = [httpReturn statusCode];
self.httpStringCodereturn = [NSHTTPURLResponse localizedStringForStatusCode:self.httpCodeReturn];
self.datosDescargados.length = 0;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.datosDescargados appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.delegado terminaDescarga:self.datosDescargados conId:self.id httpCodeReturn:self.httpCodeReturn httpStringCodereturn:self.httpStringCodereturn];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.delegado errorEnDescarga:error.code descrip:[error.userInfo objectForKey:NSLocalizedDescriptionKey ] conId:self.id];
}
#pragma mark - connectToWSArticulo
-(NSDictionary*) connectToWSArticulo:(NSString *) lstrIdArticulo //antes regrasab un nsmutabala array
{
NSString *lsStr = @"http://";
//NSString *lsStr2 = @"/WS_Articulo/ws/ArticulosValorWS/consultaExiInvb?articulo="; Pasada
NSString *lsStr2 = @"/WS_Cliente/ws/rest/datosArticuloWS/getDatosArticulo?articulo=";
//Obtengo la dirección IP a realizar la conexión
//NSString *lstrIp = [self getSettings:@"direccionIP"]; Si es que la IP se encontrara en Settings de la aplicación
//NSString *lstrIp = @"192.168.1.52:8181"; IP de la intranet(ya no se deberá de usuar)
NSString *lstrIp = @"189.210.175.215:8085";
//Concatena variables
NSString *lsCadena = [NSString stringWithFormat: @"%@%@%@%@",lsStr,lstrIp,lsStr2,lstrIdArticulo];
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:lsCadena]];
//NSArray *dictionaryArray = [self convertJsonDataDiccionary:data];// Antes regresaba un array
NSDictionary *diccionario = [self convertJsonDataDiccionary:data];
return diccionario;
}
-(NSDictionary *)convertJsonDataDiccionary:(NSData*)data //antes regresaba un nsarray
{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) {
UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:@"AVISO" message:@"No se puede conectar al servicio, intente de nuevo más tarde, gracias" delegate: nil cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
[alerta show]; }
//NSArray *array = [dictionary objectForKey:@"ecomExist"]; Aqui convertia el diccionario a array
return dictionary;
}
@end
The code to implement the class..
Code:
JFRed *red = [[JFRed alloc]init];
red.delegado = self;
[red descargar:[self.diccioPrincipal valueForKey:@"imgArt"] conId:1];