我正在创建一个显示WordPress帖子的android应用。目前,该应用程序正在显示标题和副标题(3行文本),但未显示特色图片。我也很想获得作者图像(如果可能的话),但是我更担心特色图像。我试图用谷歌搜索它,但我可能会问错问题。我也尝试获取图像路径,但是它根本不起作用。根据{{1}},特色图片包含一个ID,该ID可用于获取路径。但是,没有运气。
RecycleViewAdapter.java
JSON
postdetail.xml
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() ;
}
}
这是显示帖子的片段: Blog.java
<?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>
编辑: 我试图将这段代码添加到RecycleViewAdapter.class内,但还是没有运气
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("[…]","");
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;
}
}
编辑- 因此,有人给我发送了一些详细信息。但是我还是不明白。我上次尝试创建Android应用的时间是2014-2015年。详细信息如下:
是的,您可以从WordPress网站获取所有内容。 WordPress将博客文章图像存储在另一个表上。所以按照这个步骤
1)使用REST APi获取博客文章。它将是一个JSON数组。
2)检查每个json对象,每个对象都有这个JSON对象-链接-> WpFeaturedmedia ---> object(0)-> Href
3)此Href是博客文章的图片索引。
4)将此href传递给适配器。
5)适配器类内部再次对此href网址使用另一个改造调用
6)此改型的响应将包含博客帖子的所有图像(多种尺寸)。 response.body()。getMediaDetails()。getSizes()。getThumbnail()。getSourceUrl()
7)将其传递给glide方法(仅在adpater类内部) Glide.with(mContext) .load(response.body()。getMediaDetails()。getSizes()。getThumbnail()。getSourceUrl()) .into(imageView);
最后,适配器中的响应POJO不是您在活动中使用的响应POJO。为图片网址创建另一个POJO。
答案 0 :(得分:3)
好吧,做完一些研究之后,我发现您只需在网址末尾插入_embed
,就可以获取特色图片以及带有图片的作者简介。
这是演示网址
https://www.myfitbytes.com/wp-json/wp/v2/posts?_embed
从那里您可以在_embed
对象中获取作者数据
对于特色图片,它看起来像
答案 1 :(得分:1)
如果有可能,您可以将此插件添加到您的wordpress中 https://wordpress.org/plugins/better-rest-api-featured-images/
它将在JSON中添加一个“ better_featured_image”对象,并且您将能够在其“ source_url”字段中检索图像url。
如果不能,我建议您尝试使用here描述的内容,它将在结果JSON中嵌入很多信息,包括精选图片和作者