Sunday, 20 January 2019

How to get blog posts by label in blogger api

I using blogger api in my android app to integrate blogger content with it by using the REST APIs, as a json's objects.

I need to retrieve/filter posts by label. In most blogs the link of blog's label it usually is

https://abtallaldigital.blogspot.com/search/label/Food
https://abtallaldigital.blogspot.com/search/label/Technology

I read all api documentation and I see it's deal with Blogs, Posts, Comments, Pages, Users but there's no thing handle labels/categories in it.

There's a class BloggerAPI in the app that's used to retrieve blogs

package abtallaldigital.blogspot.com.dummyapp;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Url;

public class BloggerAPI {

    public static final String BASE_URL =
            "https://www.googleapis.com/blogger/v3/blogs/2399953/posts/";
    public static final String KEY = "THE-KEY";

    public static PostService postService = null;

    public static PostService getService() {

        if (postService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            postService = retrofit.create(PostService.class);
        }

        return postService;
    }

    public interface PostService {
        @GET
        Call<PostList> getPostList(@Url String url);
    }
}

It is used thus

  private void getData(){

    String url = BloggerAPI.BASE_URL + "?key=" + BloggerAPI.KEY;

    if(token != ""){
        url = url+ "&pageToken="+token;
    }
    if(token == null){
        return;
    }

   final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(Call<PostList> call, Response<PostList> response) {
            PostList list = response.body();
            token = list.getNextPageToken();
            items.addAll(list.getItems());
            adapter.notifyDataSetChanged();
            Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<PostList> call, Throwable t) {
            Toast.makeText(MainActivity.this,"Error occured",Toast.LENGTH_LONG).show();
            Log.i(TAG, "onFailure: "+t.toString());
        }
    });

}

I googling for how to get link of rss XML for any label and I found this result

https://example.blogspot.com/feeds/posts/default/-/label/?alt=rss

this will gets the blog posts of any label with replace the word "label" in the link



from How to get blog posts by label in blogger api

No comments:

Post a Comment