Wordpress Api返回空或空

时间:2019-01-29 11:36:23

标签: android wordpress api

我正在将wordpress网站转换为从其api检索其数据。我已经设置好一切,我的Wordpress api插件和端点。即使我在链接端点上设置了Json数据,但在运行代码时,响应仍返回null。似乎无法理解我在哪里弄错了。

Product.java

public class Product {
public static final int IMAGE_TYPE = 1;
public String title, subtitle, image;
public int type;

    public Product(int type, String title, String subtitle, String image) {
        this.title = title;
        this.subtitle = subtitle;
        this.image = image;
        this.type = type;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private ArrayList<Product> postList;
private RecyclerViewAdapter recyclerViewAdapter;
private LinearLayoutManager mLayoutManager;
public static List<WPProduct> mListPost;
private String baseURL = "http://www.callusmiller.com";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView =  findViewById(R.id.recyclerView);
        progressBar = findViewById(R.id.progressBar);
        postList = new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setHasFixedSize(true);
        getRetrofit();
        recyclerViewAdapter = new RecyclerViewAdapter(postList, MainActivity.this);
        recyclerView.setAdapter(recyclerViewAdapter);

    }

    private void getRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

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

        call.enqueue(new Callback<List<WPProduct>>() {
            @Override
            public void onResponse(Call<List<WPProduct>> call, Response<List<WPProduct>> response) {
                Log.e("mainactivyt", " response " + response.body());
                mListPost = response.body();
                progressBar.setVisibility(View.GONE);
                if (response.body() != null) {
                    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();


                        postList.add(new Product(Product.IMAGE_TYPE, response.body().get(i).getTitle().getRendered(),
                                tempdetails,
                                response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref()));

                        recyclerViewAdapter.notifyDataSetChanged();

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

            }
        });

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

RecyclerAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter {
    public List<Product> postList;
    public Context context;
    Activity activity;

    public RecyclerViewAdapter(ArrayList<Product> postList, Context context) {
        this.postList = postList;
        this.context = context;
        this.activity = (Activity) context;
    }
public class RecyclerImageViewHolder extends RecyclerView.ViewHolder{
        ImageView image;
        TextView title, subtitle;
        public RecyclerImageViewHolder (View itemView){
            super(itemView);
            this.title = itemView.findViewById(R.id.title);
            this.subtitle = itemView.findViewById(R.id.subtitle);
            this.image = itemView.findViewById(R.id.icon);
        }
}
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).I

ArrayApiInterface

public interface RetrofitArrayApi {
    @GET("android/wp-json/wp/v2/product")

}

EndPoint http://www.callusmiller.com/wp-json/wp/v2/product

0 个答案:

没有答案