JMA*_*R B 9 java android android-layout wordpress-rest-api
我正在创建一个显示WordPress帖子的Android应用程序.目前,该应用程序正在显示标题和副标题(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() ;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这是显示帖子的片段: 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("[…]","");
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: 我试图在RecycleViewAdapter.class中添加此代码,但仍然没有运气
Glide.with(mContext).load(response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl())
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
编辑 - 所以,有人给我发了一些细节.但我仍然没有得到它.我最后一次尝试创建Android应用程序是在2014-2015.以下是详细信息:
是的,您可以从WordPress网站上获取所有内容.WordPress商店博客帖子图像在另一张桌子上.所以按照这一步
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类)Glide.with(mContext).load(response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl()).into(imageView);
最后,适配器中的响应POJO不是您在活动中使用的响应POJO.为图片网址创建另一个POJO.
好吧,经过一些研究后,我发现您只需_embed在网址末尾进行连接,您就可以获得特色图像以及带有图像的作者简介。
这是演示网址
https://www.myfitbytes.com/wp-json/wp/v2/posts?_embed
Run Code Online (Sandbox Code Playgroud)
从那里您可以获取_embed对象内的作者数据
对于特色图像,它看起来像