帮助在自定义类中创建NSURLConnection

如何解决帮助在自定义类中创建NSURLConnection

| 嘿,我正在尝试使用Yahoo \的PlaceFinder对我正在制作的应用程序进行反向地理编码。问题是我需要使用NSURLConnection来调用我的数据库。因此,我决定制作一个使用用户的纬度和经度初始化的自定义类,并且仅存储包含用户所处状态的字符串变量。 更新以下代码现在可以正常工作了。 这是.h
#import <Foundation/Foundation.h>
#import \"CJSONDeserializer.h\"

@interface StateFinder : NSObject 
{
    Nsstring *userState;
    NSURLConnection *connection;
}

-(id)initwithLatitude:(Nsstring *)latitude andLongitude:(Nsstring *)longitude;
@property (nonatomic,retain) Nsstring *userState;
@property (nonatomic,retain) NSURLConnection *connection;

@end
还有他们
#import \"StateFinder.h\"

@implementation StateFinder

@synthesize userState;
@synthesize connection;

-(id)initwithLatitude:(Nsstring *)latitude andLongitude:(Nsstring *)longitude
{
    if(self = [super init])
    {
        Nsstring *lat = latitude;
        Nsstring *lon = longitude;

        Nsstring *stateURLFinder = [Nsstring stringWithFormat:@\"http://where.yahooapis.com/geocode?q=%@,+%@&gflags=R&flags=J&appid=zqoGxo7k\",lat,lon];

        //NSLog(stateURLFinder);

        NSURL *stateURL = [NSURL URLWithString:stateURLFinder];


        NSURLRequest *request = [[NSURLRequest alloc] initWithURL: stateURL];

        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        [request release];
    }
    return self;
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@\"didReceiveResponse\");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@\"didFinishLoading\");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@\"didFailWithError\");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    // Store incoming data into a string
    Nsstring *jsonString = [[Nsstring alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(jsonString);
    // Yes,this is incomplete,but I was waiting for the method to fire before going
        // any further. This will at least show me the JSON data being returned from yahoo 
        // in string format so I can output it to the console via NSLog
}

- (void)dealloc 
{
    [userState release];
    [connection release];
    [super dealloc];
}

@end
这是我正在使用的当前代码,并且工作正常。我所做的只是将connectionDidFinishLoading和didFailWithError方法包含到原始代码中。关于在建立连接之前将其释放的问题,我按原样使用上面的代码,而没有前面提到的方法,ReceiveData / didReceiveResponse都不会起作用。直到包括这两种方法时,这些方法才开始被调用。不确定如何,不确定为什么,但这是所有建议中起作用的唯一更改。非常感谢@Jiva DeVoe,@XJones,@jlehr和@Aby提供的所有提示/提示/建议!     

解决方法

实际上,我建议您绝对不要使用sendSynchronousRequest。除非您开发的应用程序是没有运行循环的命令行应用程序,否则您应该始终尝试使用异步网络。 我怀疑您的问题可能是您正在立即释放连接,因此它永远没有机会运行。您应该添加一个成员变量,并保持它不变,直到收到响应或其他任何响应为止。 奖金提示: 猜测您可能是在iOS或Mac上执行此操作,并正在编写GUI应用程序。这些应用程序具有运行循环。当您按照先前的答案所建议的那样使用同步网络时,将阻止该runloop执行。这意味着,如果请求花费的时间超过几秒钟,则您的iOS应用将被操作系统杀死,而您的Mac应用将显示为无响应。这些都不是好的结果。     ,您正在向连接发送
release
消息,否则连接将无法运行。不要那样做。关于JSON,您说的从服务器返回的字符串是JSON。您还期待其他吗?     ,摆脱这条线:
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
使用请求初始化连接后,它将自动运行。您还有其他问题。您对NSURLConnectionDelegate协议的实现是错误的。您需要将在“ 4”中接收的数据递增地添加到NSMutableData对象。您可以在“ connectionDidFinishLoading:\”中将其转换为JSON。 至于内存泄漏,请以
connectionDidFinishLoading:
connection:didFailWithError:
的方式释放连接。一定会收到一个,但不能同时收到。 您需要知道的一切都在这里 [编辑:添加了NSURLConnection代码示例]
// Attach this to the touchUpInside event of a UIButton
// Note that all objects will be autoreleased
// Note that you can comment out any or all of the NSURLConnection delegate methods
// and the request will execute

- (IBAction)initiateRequest
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@\"http://www.google.com\"]];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)date
{
    NSLog(@\"connection:didReceiveData\");
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@\"connection:didReceiveResponse:\");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@\"connectionDidFinishLoading:\");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@\"connection:didFailWithError:\");
}
    ,感谢XJones的建议。一次插入代码后,我并没有包括
didFinishLoading
didFailWithError
这两种方法,,10ѭ和
didReceiveData
都开始被使用。谢谢大家的提示,建议和建议,希望这对以后的人有所帮助。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?