java - 如何获取和显示 Wordpress 特色媒体和作者图像?

标签 java android android-layout wordpress-rest-api

我正在创建一个显示 WordPress 帖子的安卓应用程序。目前,应用程序显示标题和副标题(3 行文本),但不显示特色图片。我也很想获得作者图片(如果可能的话),但我更担心特色图片。我试着用谷歌搜索,但我可能问错了问题。我也试图获取图像路径,但它根本不起作用。根据JSON,特色图片包含一个id,这个id可以用来获取路径。但是,运气不好。

RecycleViewAdapter.java

package com.myfitbytes;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;



 public class RecyclerViewAdapter extends RecyclerView.Adapter {

private ArrayList<Model> dataset;
private Context mContext;

public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) {
    this.dataset = mlist;
    this.mContext = context;
}

public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{


    TextView title, subtitle;
    ImageView imageView;

    public ImageTypeViewHolder(View itemView) {
        super(itemView);

        this.title = (TextView)  itemView.findViewById(R.id.title);
        this.subtitle = (TextView) itemView.findViewById(R.id.subtitle);
        //at the moment, it is displaying an icon for all posts
        this.imageView = (ImageView) itemView.findViewById(R.id.Icon);
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.postdetails, parent, false);
    return new ImageTypeViewHolder(view) ;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    final Model object = dataset.get(position);

    ( (ImageTypeViewHolder) holder).title.setText( object.title );
    ( (ImageTypeViewHolder) holder).subtitle.setText( object.subtitle );

    ( (ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, WPPostDetails.class);
            intent.putExtra("itemPosition", position);
            mContext.startActivity(intent);
        }
    });
    ( (ImageTypeViewHolder) holder).subtitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, WPPostDetails.class);
            intent.putExtra("itemPosition", position);
            mContext.startActivity(intent);
        }
    });
    ( (ImageTypeViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, WPPostDetails.class);
            intent.putExtra("itemPosition", position);
            mContext.startActivity(intent);
        }
    });

    /// dataset.get(position)
}

  @Override
  public int getItemCount() {
    return dataset.size() ;
   }
}

postdetail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardElevation="5dp">




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:paddingTop="5dp"

        android:layout_weight="4">

        <ImageView
            android:id="@+id/Icon"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"
            android:layout_weight="9"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="left"
            android:id="@+id/title"
            android:textColor="@color/colorBlack"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:id="@+id/subtitle"
            android:padding="5dp"
            android:layout_marginBottom="5dp"
            android:maxLines="3"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:lineSpacingExtra="2dp"/>



</LinearLayout>

这是显示帖子的 fragment : Blog.java

    package com.myfitbytes;

   import android.os.Bundle;
   import android.support.annotation.Nullable;
   import android.support.v4.app.Fragment;
   import android.support.v7.widget.LinearLayoutManager;
   import android.support.v7.widget.RecyclerView;
   import android.util.Log;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.ProgressBar;

   import java.util.ArrayList;
   import java.util.List;

   import retrofit2.Call;
   import retrofit2.Callback;
   import retrofit2.Response;
   import retrofit2.Retrofit;
   import retrofit2.converter.gson.GsonConverterFactory;

   public class Blog extends Fragment {



private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager mLayoutManager;
private ArrayList<Model> list;
private RecyclerViewAdapter adapter;

private String baseURL = "https://www.myfitbytes.com/";

public static List<WPPost> mListPost;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    //
    LayoutInflater lf = getActivity().getLayoutInflater();
    View view = lf.inflate(R.layout.fragment_blog, container, false);


    //wordpress blog posts

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBarPosts);

    mLayoutManager = new LinearLayoutManager(getActivity(), 
    LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(mLayoutManager);

    list = new ArrayList<Model>();
    /// call retrofill
    getRetrofit();

    adapter = new RecyclerViewAdapter( list, getActivity());

    recyclerView.setAdapter(adapter);



    return view;



}



public void getRetrofit(){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
    Call<List<WPPost>>  call = service.getPostInfo();

    // to make call to dynamic URL

    // String yourURL = yourURL.replace(BaseURL,"");
    // Call<List<WPPost>>  call = service.getPostInfo( yourURL);

    /// to get only 6 post from your blog
    // http://your-blog-url/wp-json/wp/v2/posts?per_page=2

    // to get any specific blog post, use id of post
    //  http://www.blueappsoftware.in/wp-json/wp/v2/posts/1179

    // to get only title and id of specific
    // http://www.blueappsoftware.in/android/wp-json/wp/v2/posts/1179? 
     fields=id,title



    call.enqueue(new Callback<List<WPPost>>() {
        @Override
        public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> 
        response) {
            Log.e("blog", " response "+ response.body());
            mListPost = response.body();
            progressBar.setVisibility(View.GONE);
            for (int i=0; i<response.body().size();i++){
                Log.e("main ", " title "+ 
            response.body().get(i).getTitle().getRendered() + " "+
                        response.body().get(i).getId());

                String tempdetails =  
                response.body().get(i).getExcerpt().getRendered().toString();
                tempdetails = tempdetails.replace("<p>","");
                tempdetails = tempdetails.replace("</p>","");
                tempdetails = tempdetails.replace("[&hellip;]","");

                list.add( new Model( Model.IMAGE_TYPE,  
    response.body().get(i).getTitle().getRendered(),
                        tempdetails,

   response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref())  );

            }
            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFailure(Call<List<WPPost>> call, Throwable t) {

        }
    });

   }

   public static List<WPPost> getList(){
       return  mListPost;
   }


}

编辑: 我试图将此代码添加到 RecycleViewAdapter.class 中,但仍然没有成功

Glide.with(mContext).load(response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl())
                    .into(imageView);

编辑 - 所以,有人给我发了一些细节。但我还是不明白。我最后一次尝试创建 Android 应用程序是在 2014-2015 年。以下是详细信息:

是的,您可以从您的 WordPress 网站获取所有内容。 WordPress 在另一张 table 上存储博文图片。所以按照这一步

1) 使用 REST API 获取博客文章..它将是一个 JSON 数组。

2) 检查每个json对象,每个对象都有这个JSON对象- Links --> WpFeaturedmedia--->object(0)-->Href

3) 此 Href 是博客文章的图像索引。

4) 将此 href 传递给适配器..

5) 在适配器类内部再次使用此 href url 的另一个改造调用

6) 此改造的响应将包含博客文章的所有图像(多种尺寸)。 response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl()

7) 将其传递给 glide 方法(仅在 adpater 类内部) 滑行(mContext) .load( response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl()) .into( ImageView );

最后,适配器中的响应 POJO 不是您在 Activity 上使用的响应 POJO。为图像 url 创建另一个 POJO。

最佳答案

好的,在做了一些研究之后,我发现您只需在您的网址末尾连接 _embed,您就可以获得特色图片以及带图片的作者简介。

这是演示地址

https://www.myfitbytes.com/wp-json/wp/v2/posts?_embed 

然后您可以从那里获取 _embed 对象中的作者数据

enter image description here

对于特色图片,它看起来像

enter image description here

enter image description here

关于java - 如何获取和显示 Wordpress 特色媒体和作者图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52428257/

相关文章:

java - 关于简单客户端服务器套接字应用程序的一些问题

java - Silk Test Open Agent 在测试执行后不会自动停止

android - Sceneform 的 Gradle 问题 - 程序类型已存在 : com. google.ar.schemas.lull

android - 如何给listView这个阴影样式?

java - 斯坦福神经网络依赖解析器 : Unrecoverable error while loading a tagger model

java - 如何将 JTree 中的选择限制为特定类型的节点?

java - 将从 Google Tasks API 返回的 Android 中的 RFC 3339 日期时间对象转换为字符串

android - 想要在 Android 问答游戏结束时显示正确答案和玩家选择的答案

android - ActionBar - PullToRefresh

android - 如何通过单击 ListView 上的项目从 ListView 中获取数据