• I Have show a my web posts in my Android App. I using io.realm to show my post list. And now, i want to show my pages in my webapp. But how to use the code?
    This is my code to show my post in android app using the io.realm in table java.

    package com.app.wordpress.realm.table;
    
    import java.util.ArrayList;
    
    import com.app.wordpress.model.Post;
    import io.realm.RealmList;
    import io.realm.RealmObject;
    import io.realm.annotations.PrimaryKey;
    
    public class PostRealm extends RealmObject {
    
        @PrimaryKey
        public int id = -1;
        public String type = "";
        public String slug = "";
        public String url = "";
        public String status = "";
        public String title = "";
        public String title_plain = "";
        public String content = "";
        public String excerpt = "";
        public String date = "";
        public String modified = "";
        public String thumbnail = "";
        public int comment_count = -1;
    
        public long added_date = 0;
    
        public RealmList<CategoryRealm> categories;
        public AuthorRealm author = new AuthorRealm();
        public RealmList<CommentRealm> comments;
        public RealmList<AttachmentRealm> attachments;
    
        public Post getOriginal() {
            Post p = new Post();
            p.id = id;
            p.type = type;
            p.slug = slug;
            p.url = url;
            p.status = status;
            p.title = title;
            p.title_plain = title_plain;
            p.content = content;
            p.excerpt = excerpt;
            p.date = date;
            p.modified = modified;
            p.thumbnail = thumbnail;
            p.comment_count = comment_count;
    
            p.categories = new ArrayList<>();
            for (CategoryRealm c : categories) {
                p.categories.add(c.getOriginal());
            }
    
            p.comments = new ArrayList<>();
            for (CommentRealm c : comments) {
                p.comments.add(c.getOriginal());
            }
    
            p.author = author.getOriginal();
    
            p.attachments = new ArrayList<>();
            for (AttachmentRealm a : attachments) {
                p.attachments.add(a.getOriginal());
            }
    
            return p;
        }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not a Java dev, but it’s clear to me your posted code isn’t what is actually getting data from your site. It’s not clear even what method is used. The most common and recommended is the REST API. To get a JSON list of pages through the API, do a request similar to http://demo.wp-api.org/wp-json/wp/v2/pages. This differs from a posts request only in that we use “pages” instead of “posts” in the request. Assuming your app is using the API, whatever you need to do to alter the request as demonstrated should result in pages being displayed instead of posts.

    I’m guessing something needs to be changed in the import com.app.wordpress.model.Post. If there is not a com.app.wordpress.model.Page, you probably need to create one.

    Thread Starter ghoys

    (@ghoys)

    I using API for my app. This the API to get the connectin into homepage.

    package com.app.wordpress.connection;
    
    import com.app.wordpress.connection.callbacks.CallbackCategories;
    import com.app.wordpress.connection.callbacks.CallbackCategoryDetails;
    import com.app.wordpress.connection.callbacks.CallbackComment;
    import com.app.wordpress.connection.callbacks.CallbackDetailsPost;
    import com.app.wordpress.connection.callbacks.CallbackDevice;
    import com.app.wordpress.connection.callbacks.CallbackInfo;
    import com.app.wordpress.connection.callbacks.CallbackListPost;
    import com.app.wordpress.model.DeviceInfo;
    
    import retrofit2.Call;
    import retrofit2.http.Body;
    import retrofit2.http.GET;
    import retrofit2.http.Headers;
    import retrofit2.http.POST;
    import retrofit2.http.Query;
    
    public interface API {
    
        /* your wordPress url */
        String BASE_URL = "http://bogorone.co.id/";
    
        // minimize field for list of post
        String EXCLUDE_FIELD = "&exclude=content,categories,tags,comments,custom_fields";
    
        /* info API transaction ------------------------------- */
    
        @GET("?json=info")
        Call<CallbackInfo> getInfo();
    
        /* Post API transaction ------------------------------- */
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=get_posts" + EXCLUDE_FIELD)
        Call<CallbackListPost> getPostByPage(
                @Query("page") int page,
                @Query("count") int count
        );
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=get_post")
        Call<CallbackDetailsPost> getPostDetialsById(
                @Query("id") int id
        );
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=get_search_results" + EXCLUDE_FIELD)
        Call<CallbackListPost> getSearchPosts(
                @Query("search") String search,
                @Query("count") int count
        );
    
        /* Category API transaction --------------------------- */
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=get_category_index")
        Call<CallbackCategories> getAllCategories();
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=get_category_posts" + EXCLUDE_FIELD)
        Call<CallbackCategoryDetails> getCategoryDetailsByPage(
                @Query("id") int id,
                @Query("page") int page,
                @Query("count") int count
        );
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @GET("?json=respond/submit_comment")
        Call<CallbackComment> sendComment(
                @Query("post_id") long post_id,
                @Query("name") String name,
                @Query("email") String email,
                @Query("content") String content
        );
    
        @Headers({"Cache-Control: max-age=0", "User-Agent: WordPress"})
        @POST("?api-fcm=register")
        Call<CallbackDevice> registerDevice(@Body DeviceInfo deviceInfo);
    
    }
    

    The realm is only to fit what my web is appearing. But there is familiar code like slug for post type..

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Show WordPress Pages on Android App’ is closed to new replies.