gpt4 book ai didi

iphone - iOS:为什么不显示来自 HTTP Post 的 responseData

转载 作者:行者123 更新时间:2023-11-28 19:22:44 26 4
gpt4 key购买 nike

我在 stackoverflow 上找到了很多关于设置对我的服务器的 HTTP POST 请求的好建议。我遵循了很多例子。我在一个项目中设置了一个快速测试,试图发送一个 POST 请求并从服务器检索一些数据。这是我设置的 ViewController.h 中的代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
IBOutlet UILabel *label;
NSString *moviePost;
NSMutableData *httpResponse;
NSURLConnection *connection;

}

@property (nonatomic, retain) NSString *moviePost;
@property (nonatomic, retain) NSURLConnection *connection;

@end

还有来 self 的 ViewController.m 的代码

#import "ViewController.h"
#import <YAJLiOS/YAJL.h>

@implementation ViewController

@synthesize moviePost;
@synthesize connection;

- (void) viewDidLoad {
[super viewDidLoad];

NSArray *fields = [[NSArray alloc] initWithObjects:@"rating",@"genre",@"plotoutline",@"runtime",@"director",@"writer",@"mpaa",@"year",@"studio", nil];

NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
fields,@"fields",
nil];
[fields release];
NSDictionary *tempPost = [[NSDictionary alloc] initWithObjectsAndKeys:
@"2.0", @"jsonrpc",
@"VideoLibrary.GetMovies", @"method",
@"1", @"id",
params, @"params",
nil];
[params release];
moviePost = [tempPost yajl_JSONString];
[tempPost release];


NSLog(@"Going to post: %@\n\n", moviePost);

NSString *url = [NSString stringWithFormat:@"http://192.168.1.133:8080/jsonrpc"];

NSMutableString* requestURL = [[NSMutableString alloc] init];
[requestURL appendString:url];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];

NSData *requestData = [NSData dataWithBytes: [moviePost UTF8String] length: [moviePost length]];

[request setHTTPMethod: @"POST"];
[request setValue:@"text/plain" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: requestData];

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

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[httpResponse setLength:0];
}

// Called when data has been received
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[httpResponse appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString* responseString = [[NSString alloc] initWithData:httpResponse encoding:NSUTF8StringEncoding];

// Do something with the response
NSLog(@"Response: %@", responseString);

[responseString release];
[connection release];
[httpResponse release];
}

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

大部分代码来 self 找到的示例。向服务器发送 POST 请求确实有效,并且服务器确实以数据响应。我已经通过在测试设备和服务器之间进行数据包捕获来证明这一点。发送正确的请求并发回正确的响应。运行应用程序时我没有遇到任何问题。我已经运行了分析器,但没有遇到任何问题。

我遇到的问题是我无法在 NSLog 中获得打印响应。什么时候- (void)connectionDidFinishLoading:(NSURLConnection *)connection 运行它确实在 NSLog 中显示一行 Response: ,但这就是我得到的。

有什么我想念的吗?我是 objective-c 和 xcode 的新手。感谢您的帮助。

最佳答案

albertamg 在评论中指出了这一点。

你需要 allocinit 你的 NSMutableData *httpResponse 除非你在其他地方完成了并且没有在上面的代码中显示它.

你应该在你的 viewDidLoad 方法中添加它

httpResponse = [[NSMutableData alloc] init];

关于iphone - iOS:为什么不显示来自 HTTP Post 的 responseData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com