- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 IOS 编程的新手,想将用户的位置发送到服务器。我在 Internet 上关注了 iOS 开发人员库和教程,但运行代码时没有任何反应。我正在使用 iOS 模拟器,它很好地为我提供了模拟位置,并且我得到了 NSURLConnection
实例。但是,服务器端没有任何反应。
#import "JLSViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "JLSEventData.h"
@interface JLSViewController ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSMutableArray *locations;
@end
@implementation JLSViewController
NSMutableData *responseData = nil;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locations = [[NSMutableArray alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)enabledStateChanged:(id)sender
{
if (self.switchEnabled.on)
{
[self.locationManager startUpdatingLocation];
}
else
{
[self.locationManager stopUpdatingLocation];
}
}
- (void)beginBackgroundUpdateTask: (CLLocation *)location {
}
- (void)endBackgroundUpdateTask {
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (newLocation == nil) {
return;
}
if (oldLocation != nil) {
if (newLocation.coordinate.latitude == oldLocation.coordinate.latitude &&
newLocation.coordinate.longitude == oldLocation.coordinate.longitude) {
return;
}
}
NSError *error = nil;
JLSEventData *eventData = [[JLSEventData alloc] initWithLocation: newLocation];
NSString *strData = [eventData toJSON:error];
if (error != nil){
return;
}
if (self.locations.count>5)
[self.locations removeObjectAtIndex:0];
[self.locations addObject:strData];
NSData *requestBodyData = [NSJSONSerialization dataWithJSONObject:self.locations options:NSJSONWritingPrettyPrinted error:&error];
if (error != nil){
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//[self beginBackgroundUpdateTask:newLocation];
// Send a synchronous request
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: "http://localhost:8080/mywebservice"]];
[urlRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setHTTPMethod:@"POST"];
//[urlRequest setTimeoutInterval:20];
//NSJSONSerialization dataWithJSONObject
urlRequest.HTTPBody = requestBodyData;
// Create url connection and fire request
[urlRequest setHTTPBody:[NSData dataWithBytes:[strData UTF8String] length:strlen([strData UTF8String])]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (conn!=nil){
//receivedData = nil;
}
});
}
#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"==>didReceiveResponse");
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"==>didReceiveData");
// Append the new data to the instance variable you declared
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSLog(@"Succeeded! Received %d bytes of data",[responseData length]);
responseData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
/*
*
*/
- (void)parseData {
//Do something
}
@end
如有任何帮助,我们将不胜感激。
最佳答案
您正在一个单独的线程上执行 NSURLConnection。并且线程在收到响应之前退出。您要么需要在主线程上执行 NSURLConnection,要么需要保持线程运行直到收到响应。
关于IOS7 NSURLConnection 没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181932/
我正在尝试将高斯模糊添加到场景中。我正在使用此GLSL着色器:。当您提供图像作为tDiffuse纹理时,它对图像起作用,但我正在尝试将其添加为后处理效果。我试图获取摄影机的renderTarget并将
我正在做一款天气应用程序,到目前为止还不错。现在,我想通过单击按钮来呈现一些额外的信息。我有一个5天的预报,然后想显示每一天的信息。我已经成功地过滤了数据,但无法获得要渲染的数据地图。以下是我的一些代
我正在做一款天气应用程序,到目前为止还不错。现在,我想通过单击按钮来呈现一些额外的信息。我有一个5天的预报,然后想显示每一天的信息。我已经成功地过滤了数据,但无法获得要渲染的数据地图。以下是我的一些代
我在全球安装了Create Reaction应用程序。然后我就跑了。NPX创建-反应-应用JSX。它安装了大约1460个包的所有包,但没有设置Public和src文件夹。在我的JSX文件夹中,只有1:
我是一名优秀的程序员,十分优秀!