ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作
<p><p>我正在制作一个 jsonstring。当我执行它时,当我在浏览器中执行它时它就可以工作。我通过记录确切的 url 并将其复制到浏览器中来做到这一点。比我得到我想要的 HTTP Get,但在 iPhone 中我只得到一个错误的登录。</p>
<pre><code>- (IBAction)getDown:(id)sender { //perform get request
NSLog(@"beginnen met versturen");
//NSString * _barCode = [ objectForKey:@"phoneNumber"];
//build up the request that is to be sent to the server
//NSString*jsonString = [ initWithFormat:@"{\"barcode\":\"%@\"}", _barCode];
NSString*jsonString = [ initWithFormat:@"{\"barcode\":\"123456\"}"];
NSString *str = ;
NSLog(@"%@", str);
NSMutableURLRequest *request = [ initWithURL:];
NSLog(@"url: %@", request);
;
// ; //selects what task the server will perform
NSLog(@"met value: %@", request);
//initialize an NSURLConnectionwith the request
NSURLConnection *connection = [ 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 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);
//;//send the data to the delegate
NSData *data = receivedData;
NSDictionary *dictionary = ;
NSLog(@"%@",dictionary.JSONString ); ; // set the textview to the raw string value of the data recieved
NSString *value1 = ;
NSLog(@"%@", value1);
NSString *value2 = ;
NSLog(@"%@",dictionary);
NSLog(@"%@", value2);
}
</code></pre>
<p>这是日志:</p>
<pre><code>2013-01-10 16:31:46.550 Scanner http://server.nl/scan.php?data={"barcode":"123456"}
2013-01-10 16:31:46.551 Scanner url: <NSMutableURLRequest (null)>
2013-01-10 16:31:46.553 Scanner met value: <NSMutableURLRequest (null)>
**2013-01-10 16:31:46.556 Scanner Connection failed with error: bad URL**
</code></pre>
<p>当我从字符串中删除完整的 json 时,我没有得到错误的 url。所以可能有问题。有谁知道我做错了什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在执行 URL 请求之前,您需要对其进行编码。
最好和最优雅的解决方案是在 NSString 上添加一个类别,例如:</p>
<pre><code>- (NSString*)URLEncode {
// Should not be encoded:-_.
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(";/?:@&=+$,!*'()<>#%\"{}|\\^[]`~"), kCFStringEncodingUTF8) autorelease];
//
}
</code></pre>
<p>当你提出请求时:</p>
<pre><code>NSString *str = ;
NSMutableURLRequest *request = [ initWithURL:];
</code></pre>
<p>如果您不想使用其他文件(甚至认为会推荐),请将此方法添加到您的类中:</p>
<pre><code>- (NSString*)URLEncode:(NSString )yourURL {
// Should not be encoded:-_.
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(";/?:@&=+$,!'()<>#%\"{}|\\^[]`~"), kCFStringEncodingUTF8) autorelease];
}
</code></pre>
<p>并使用</p>
<pre><code>NSMutableURLRequest *request = [ initWithURL:];
</code></pre></p>
<p style="font-size: 20px;">关于ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14261548/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14261548/
</a>
</p>
页:
[1]