私はjsonstringを作っています。実行すると、ブラウザで実行すると機能します。これを行うには、正確な URL をログに記録し、それをブラウザーにコピーします。必要なHTTP Getを取得するよりも、iPhoneではBad Loginしか取得しません。
- (IBAction)getDown:(id)sender { //perform get request
NSLog(@"beginnen met versturen");
//NSString * _barCode = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
//build up the request that is to be sent to the server
//NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"barcode\":\"%@\"}", _barCode];
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"barcode\":\"123456\"}"];
NSString *str = [NSString stringWithFormat: @"http://server.nl/scan.php?data=%@",jsonString];
NSLog(@"%@", str);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:str]];
NSLog(@"url: %@", request);
[request setHTTPMethod:@"GET"];
// [request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; //selects what task the server will perform
NSLog(@"met value: %@", request);
//initialize an NSURLConnection with the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // executed when the connection receives data
receivedData = data;
/* NOTE: if you are working with large data , it may be better to set recievedData as NSMutableData
and use [receivedData append:Data] here, in this event you should also set recievedData to nil
when you are done working with it or any new data received could end up just appending to the
last message received*/
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //executed when the connection fails
NSLog(@"Connection failed with error: %@",error.localizedDescription);
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
/*This message is sent when there is an authentication challenge ,our server does not have this requirement so we do not need to handle that here*/
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Request Complete,recieved %d bytes of data",receivedData.length);
//[self.delegate requestReturnedData:receivedData];//send the data to the delegate
NSData *data = receivedData;
NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
NSLog(@"%@",dictionary.JSONString ); ; // set the textview to the raw string value of the data recieved
NSString *value1 = [dictionary objectForKey:@"barcode"];
NSLog(@"%@", value1);
NSString *value2 = [dictionary objectForKey:@"product"];
NSLog(@"%@",dictionary);
NSLog(@"%@", value2);
}
ログは次のとおりです。
2013-01-10 16:31:46.550 Scanner[14875:907] http://server.nl/scan.php?data={"barcode":"123456"}
2013-01-10 16:31:46.551 Scanner[14875:907] url: <NSMutableURLRequest (null)>
2013-01-10 16:31:46.553 Scanner[14875:907] met value: <NSMutableURLRequest (null)>
**2013-01-10 16:31:46.556 Scanner[14875:907] Connection failed with error: bad URL**
文字列から完全なjsonを削除すると、悪いURLは取得されません。そのため、問題がある可能性があります。誰が私が間違っているのか知っていますか?