Ionic 3 RSS read with rss2json “Unprocessable Enti

2020-05-06 16:05发布

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 "}

Code:

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);
      }

    );
  }
}

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;
      }

    }

Error -->

Error

1条回答
你好瞎i
2楼-- · 2020-05-06 16:50

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

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

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

now some details:

  • when you configure a URL using rss2json it basically adds parameters to the original URL, example:

https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Ftechcrunch.com%2Ffeed%2F&api_key=q5ijkolkdjk3urzrcfaehxeoimxr3tdu5ieiqcrq&order_by=pubDate&order_dir=asc&count=20

  • So in Angular/Ionic you need to leverage Angular's HttpParams to properly form request, here is your provider code with HttpParams:

provider code:

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);
  }

}
查看更多
登录 后发表回答