ios - http请求没有进入服务器

时间:2014-03-26 22:23:20

标签: ios objective-c http rest nsurlconnection

我正在尝试从模拟器上的ios应用程序向我在本地服务器上运行的REST服务发送http请求。我在这里关注了一些关于如何设置简单请求的帖子,但它似乎并没有像REST服务器那样。我是新手,所以我并不是我出错的地方。

更新:原来这实际上是在联系服务器。问题似乎与请求的MIME类型有关。

更新2:我将NSURLRequest更改为NSMutableURLRequest,以便我可以包含此行[request setValue:@"text/html" forHTTPHeaderField: @"Content-Type"]; 将内容类型更改为html。现在,当我尝试发送请求时,我收到错误。 我在输出日志中收到了这个

:2014-03-26 23:48:07.510 REST[1516:70b] -[NSURLRequest setValue:forHTTPHeaderField:]: unrecognized selector sent to instance 0x8a1abe0
2014-03-26 23:48:07.713 REST[1516:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest setValue:forHTTPHeaderField:]: unrecognized selector sent to instance 0x8a1abe0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173a5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bd8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017d7903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0172a90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0172a4ee _CF_forwarding_prep_0 + 14
    5   REST                                0x00001ead -[ViewController fetchTweet] + 237
    6   REST                                0x0000223e -[ViewController go:] + 78
    7   libobjc.A.dylib                     0x014cf874 -[NSObject performSelector:withObject:withObject:] + 77
    8   UIKit                               0x0022d0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    9   UIKit                               0x0022d04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    10  UIKit                               0x003250c1 -[UIControl sendAction:to:forEvent:] + 66
    11  UIKit                               0x00325484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    12  UIKit                               0x00324733 -[UIControl touchesEnded:withEvent:] + 641
    13  UIKit                               0x0026a51d -[UIWindow _sendTouchesForEvent:] + 852
    14  UIKit                               0x0026b184 -[UIWindow sendEvent:] + 1232
    15  UIKit                               0x0023ee86 -[UIApplication sendEvent:] + 242
    16  UIKit                               0x0022918f _UIApplicationHandleEventQueue + 11421
    17  CoreFoundation                      0x016c383f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    18  CoreFoundation                      0x016c31cb __CFRunLoopDoSources0 + 235
    19  CoreFoundation                      0x016e029e __CFRunLoopRun + 910
    20  CoreFoundation                      0x016dfac3 CFRunLoopRunSpecific + 467
    21  CoreFoundation                      0x016df8db CFRunLoopRunInMode + 123
    22  GraphicsServices                    0x036df9e2 GSEventRunModal + 192
    23  GraphicsServices                    0x036df809 GSEventRun + 104
    24  UIKit                               0x0022bd3b UIApplicationMain + 1225
    25  REST                                0x00002a2d main + 141
    26  libdyld.dylib                       0x01d78701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是ViewController.h文件:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ViewController : UIViewController<NSURLConnectionDelegate>

@property (nonatomic, readwrite, weak) IBOutlet UIButton *button;
@property (nonatomic, readwrite, weak) IBOutlet UILabel *label;

@property (nonatomic, strong) IBOutlet UILabel *tweetId;
@property (nonatomic, strong) IBOutlet UILabel *tweetContent;

@property (nonatomic) NSURLConnection *connection;


- (IBAction)go:(id)sender;
- (IBAction)fetchTweet;


@end

这是ViewController.m文件:

#import "ViewController.h"

@implementation ViewController

@synthesize button=_button;
@synthesize label=_label;
@synthesize tweetId=_tweetId;
@synthesize tweetContent=_tweetContent;
@synthesize connection=_connection;

- (IBAction)fetchTweet
{
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //NSLog(@"Did Receive Data %@", data);
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Did Fail");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Did Finish");
    // Do something with responseData
}

- (IBAction)go:(id)sender
{
    [self fetchTweet];
    self.label.text = @"Working";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

问题在于您只是忽略了进来的数据,从我看到您的服务器到达的评论中,如果您将实现更新为类似的内容,它应该可以正常工作

#import "ViewController.h"

@implementation ViewController
{
    NSMutableData *_currentData;
}

@synthesize button=_button;
@synthesize label=_label;
@synthesize tweetId=_tweetId;
@synthesize tweetContent=_tweetContent;
@synthesize connection=_connection;

- (IBAction)fetchTweet
{
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];
    self->_currentData = [[NSMutableData alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //NSLog(@"Did Receive Data %@", data);
   [self->_currentData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Did Fail");
    self->_currentdata = Nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Did Finish");
    // Do something with responseData
    NSString *dataString = [[NSString alloc] initWithdata:self->_currentData encoding:NSASCIIStringEncoding]
    NSLog(@"Received data: %@", dataString);
}

- (IBAction)go:(id)sender
{
    [self fetchTweet];
    self.label.text = @"Working";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end