gpt4 book ai didi

ios - json NSJSONSerialization 无法加载,因为数据是 html

转载 作者:行者123 更新时间:2023-11-29 01:28:45 27 4
gpt4 key购买 nike

我得到以下 _downloadedData:

<!-- pageok -->
<!-- managed by puppet -->
<html>
<pre>pageok</pre>
</html>

使用下面列出的代码时:

- (void)downloadItems
{
//Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://www.scratchclass.com:80/videos/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:jsonFileUrl];
[request setHTTPMethod:@"GET"];
[request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}

}


#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse: %@",response);
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[_downloadedData appendData:data];

NSString* newStr = [[NSString alloc] initWithData:_downloadedData encoding:NSUTF8StringEncoding ];
NSLog(@"didReceiveData: %@",newStr);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the videos
NSMutableArray *_videos = [[NSMutableArray alloc] init];

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];

// Create a new video object and set its props to JsonElement properties
GetVideos1 *testVideo = [[GetVideos1 alloc] init];
testVideo.IDVideo = jsonElement[@"IDVideo"];
testVideo.title = jsonElement[@"title"];

// Add this question to the locations array
[_videos addObject:testVideo];
}

// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_videos];
}
}

json其实是:

[{"IDVideo":"6Joup252fR0","title":"Top 30 Baseball Plays"},{"IDVideo":"aAy3Sh_RXjc","title":"MLB top plays"},{"IDVideo":"bkiaAGOoLjc","title":"Top 50 most unforgettable ejections"}]

但我无法获取数据,因为它是#text 节点名称的一部分。如何获取数据?

这是使用的 php 代码:

<?php 
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('idirectorySQL.db.9960960.hostedresource.com', 'idirectorySQL', 'iDirectory70!', 'idirectorySQL');
$this->db->autocommit(FALSE);
}
$con=mysqli_connect('idirectorySQL.db.9960960.hostedresource.com', 'idirectorySQL', 'iDirectory70!', 'idirectorySQL');
// This SQL statement selects ALL from the table 'videos'
$sql = "SELECT IDVideo,title FROM videos";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object()) {
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Close connections
mysqli_close($con);
// Finally, encode the array to JSON and output the results
header('Content-type: application/json');
// echo json_encode($resultArray, JSON_PRETTY_PRINT);
echo json_encode($resultArray);
}
?>

我用过

header('Content-type:application/x-www-form-urlencoded')

但是我还是收到了同样的信息。此外,这是从请求 header 中读取的内容:

Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

最佳答案

不要在 NSURLRequest 中设置任何自定义参数,GET 无论如何都是默认参数。

由于 NSURLConnection 已弃用,这是使用推荐的 NSURLSession API 的解决方案

NSURL *url = [NSURL URLWithString:@"http://www.scratchclass.com:80/videos/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
if (error) {
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
} else {
NSError *jsonError;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
NSLog(@"NSJSONSerialization failed! Error - %@ %@",
[jsonError localizedDescription],
[[jsonError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
} else {
NSMutableArray *videos = [[NSMutableArray alloc] init];
for (NSDictionary *item in jsonArray) {
GetVideos1 *testVideo = [[GetVideos1 alloc] init];
testVideo.IDVideo = item[@"IDVideo"];
testVideo.title = item[@"title"];

// Add this question to the locations array
[videos addObject:item];
}
if (self.delegate) {
[self.delegate itemsDownloaded:videos];
}
}
}
}] resume];

关于ios - json NSJSONSerialization 无法加载,因为数据是 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716776/

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