用rss2json读取的Ionic 3 RSS&“不可处理的实体" [英] Ionic 3 RSS read with rss2json "Unprocessable Entity"

查看:52
本文介绍了用rss2json读取的Ionic 3 RSS&“不可处理的实体"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ionic 3上使用rrs2json API将RSS转换为JSON时遇到问题.如果执行代码,则会给我错误->响应{_body:"{" status:" error,"消息":需要 rss_url 参数."},状态:422,确定:false,statusText:"不可处理的实体}

I'm having trouble converting RSS to JSON using the rrs2json API with Ionic 3. If I execute the code it gives me the error --> Response {_body: "{" status ":" error "," message ":" rss_url parameter is required."} ", Status: 422, ok: false, statusText:" Unprocessable Entity "}

代码:

noticies.ts

noticies.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RssProvider } from '../../providers/rss/rss';

@IonicPage()
@Component({
  selector: 'page-noticies',
  templateUrl: 'noticies.html',
})
export class NoticiesPage {
rssDataArray: any = [];

  constructor(public navCtrl: NavController, public navParams: NavParams, public rssProvider: RssProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad NoticiesPage');
    this.Get_RSS_Data()
  }

  Get_RSS_Data(){
    this.rssProvider.GetRSS().subscribe(
      data => {
        this.rssDataArray = data;
        console.log(data);
      }

    );
  }
}

提供者-> rss-> rss.ts

providers --> rss --> rss.ts

  import { Injectable } from '@angular/core';
    import {Http} from '@angular/http';
    import 'rxjs/add/operator/map';

    @Injectable()
    export class RssProvider {

      constructor(public http: Http) {
        console.log('Hello RssProvider Provider');
      }

      GetRSS(){
        const RSS_URL: any='http://rss.cnn.com/rss/edition.rss';
        const API: any='XXXXXXXXXXXXXX';
        const count: any =20;
        const API_URL: any ='https://api.rss2json.com/v1/api.json';
        const response = this.http.post(API_URL, {'rss_url': RSS_URL,'api_key': API, 'count': count}).map(res => res.json());

        return response;
      }

    }

错误->

错误

推荐答案

好的.我向rss2json服务注册了自己,并确保该解决方案确实有效(您可以在控制台中看到数据).

Alright. I registered myself with the rss2json service and made sure this solution actually works (you can see the data in console).

您遇到的问题是您没有使用正确的方法来与HttpParams一起形成http请求.

The issue you have is that you are not using a proper way to form http request with HttpParams.

这里正在使用我的密钥进行Stackblitz工作: https://stackblitz.com/edit/ionic-jdwqjg

Here is working stackblitz that uses my key: https://stackblitz.com/edit/ionic-jdwqjg

现在有一些细节:

  • 当您使用rss2json配置URL时,它基本上会向原始URL添加参数,例如:

  • 因此,在Angular/Ionic中,您需要利用 Angular的HttpParams 正确地形成请求,这是您使用HttpParams提供的代码:
    • So in Angular/Ionic you need to leverage Angular's HttpParams to properly form request, here is your provider code with HttpParams:
    • 提供商代码:

      import { Injectable } from '@angular/core';
      import { HttpClient, HttpParams } from '@angular/common/http';
      
      @Injectable()
      export class RssProvider {
      
        private API_URL: string;
      
        constructor(public http: HttpClient) {
          this.API_URL = "https://api.rss2json.com/v1/api.json";
        }
      
        GetRSS() {
          const params = { params: new HttpParams().set('rss_url', 'http://rss.cnn.com/rss/edition.rss').set('api_key','q5ijkolkdjk3urzrcfaehxeoimxr3tdu5ieiqcrq').set('order_by', 'pubDate').set('order_dir', 'asc')
          }
          return this.http.get(this.API_URL, params);
        }
      
      }
      

      这篇关于用rss2json读取的Ionic 3 RSS&“不可处理的实体"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆