Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 6 của 6
  1. #1

    Làm sao để convert HTML trong iOS ???

    Hiện tại em muốn làm 1 app duyệt forum như kiểu tinhte, bắt tay vào tìm hiểu thì gặp vấn đề: dùng AFNetworking parse dữ liệu về JSON thì nó báo server ko trả về dữ liệu JSON mà chỉ là text/html ( do em ko phải người làm forum đó :'( ), giờ thì em nên làm thế nào đây ?

    Mã:
    NSURL *url = [NSURL URLWithString:@"http://kites.vn/51-1/tv-series.html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSLog(@"JSON data: %@", JSON);
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id    responseObject){
            
            NSLog(@"Request failed with error: %@, %@",error,error.userInfo);
            NSLog(@"header field of response: %@", [response allHeaderFields]);
        }];
        [operation start];
    lỗi trả về:

    Mã:
    DemoAFNetworking[698:c07] Request failed with error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
        "text/json",
        "application/json",
        "text/javascript"
    )}, got text/html" UserInfo=0x767efb0 {NSLocalizedRecoverySuggestion=<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.mes/fb#">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Kites-TV Series -  We Fly</title>......

    View more random threads:


  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi Noob_
    Hi bạn,

    Nếu format trả về không phải json thì bạn có thể dùng RequestOperation bình thường [IMG]images/smilies/smile.png[/IMG]

    Mã:
    //Tạo request với url là link tới forum
    NSURLRequest *afRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kites.vn/51-1/tv-series.html"]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
      
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response:%@", operation.responseString);
        //Bạn parse HTML (operation.responseString) ra là lấy được thông tin.
        //Lib để parser html thì nhiều: mình google ra cái này, bạn xem thử nhé
        //https://github.com/zootreeves/Objective-C-HMTL-Parser
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // xử lý khi request không thành công
    }];
    [operation start];
    Tốt nhất là bạn kêu người dev server viết các api trả về json để bạn lấy dữ liệu [IMG]images/smilies/smile.png[/IMG]

    Chúc bạn thành công.[IMG]images/smilies/2.gif[/IMG]
    Làm theo cách của bạn thì đúng là parse đc nhưng đúng là mệt thật, inspect element lòi mắt :'(, chắc phải nhờ admin viết API thôi :'(

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hi bạn,

    Nếu format trả về không phải json thì bạn có thể dùng RequestOperation bình thường [IMG]images/smilies/smile.png[/IMG]

    Mã:
    //Tạo request với url là link tới forum
    NSURLRequest *afRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kites.vn/51-1/tv-series.html"]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
      
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response:%@", operation.responseString);
        //Bạn parse HTML (operation.responseString) ra là lấy được thông tin.
        //Lib để parser html thì nhiều: mình google ra cái này, bạn xem thử nhé
        //https://github.com/zootreeves/Objective-C-HMTL-Parser
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // xử lý khi request không thành công
    }];
    [operation start];
    Tốt nhất là bạn kêu người dev server viết các api trả về json để bạn lấy dữ liệu [IMG]images/smilies/smile.png[/IMG]

    Chúc bạn thành công.[IMG]images/smilies/2.gif[/IMG]

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Ồ... trường hợp này bạn phải add một cái gọi là accept type cho request header từ AFNetworking nhé
    !

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi mrqgenius
    Ồ... trường hợp này bạn phải add một cái gọi là accept type cho request header từ AFNetworking nhé
    !
    mình add thử accept type mới nhưng ko ăn thua, vấn đề là server chỉ trả về kiểu text/html nên có sửa accept type thì vẫn chỉ nhận đc data dạng HTML thôi ^^!

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Nếu không ngại vấn đề hiệu suất bạn thử làm một cái proxy trên host của bạn để ép nó về dữ liệu json rồi truy xuất qua host của bạn xem sao.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •