Skip to main content

WP Android App using REST and volley part2

In last part of WP Android App using REST and volley tutorial we have learned to set up the basic UI of our WP app. In this part we will learn how to implement the below two features for our WP app.


  • Retrieve the sort description of the post and show in WP app

  • Show the featured Image in WP app



So first we will start by retrieving the short description of the post and show that description in our WP app.

In last tutorial we only show the title of the post, but we know showing only short title will not describe the post very well, so we should show some short description about the post. For this we don't need to do much work just need to retrieve the excerpt tag from the JSON Object that we have already fetched during retrieving the list of all post for our WP android App.

Below is the link of the series or Article of WP android App



Get the excerpt content from JSON Object



For getting the excerpt from the post list JSON Object and store it in variable we need to follow the below steps.

First of all we need to add new variable in our post.java file and set the get and set method.

String excerpt;
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}


Now retrieve and set the value of excerpt into variable using below two-line of code, below code we have to add in the same method where we retrieve the title of the post.


//Get excerpt
JSONObject exerptObj=obj.getJSONObject("excerpt");
post.setExcerpt(exerptObj.getString("rendered"));


 

Further, we have to show that value in our app also, so we need to add a TextView to the our PostItem layout xml file as given below.

<TextView
android:id="@+id/excerpt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textIsSelectable="true"
android:textSize="14sp"
android:visibility="visible" />


 

At last we need to update our post item adapter file that is PostAdapter.java file, add the below line in our VideoViewHolder and declare the variable.


protected TextView vExcerpt;
vExcerpt=(TextView) v.findViewById(R.id.excerpt);


Then set the value to our text view.

 

((VideoViewHolder)holder).vExcerpt.setText(Html.fromHtml(post.getExcerpt()));


Important point to notice here is, we are setting the Html.fromHtml inside setText method , it is because excerpt are returned in the html format.

Get the featured Image of the Post in WP App



So far we have done with basic layout of the our WP App and now we want to set the featured image of the post to make it more attractive.

So for adding the featured Image we need to do two work.


  • First retrieve the featured Image

  • Set the featured image into the post



So for retrieving the featured image we need to modify getPost method of MainActivit.java file as given below.

Here we suppose that you have already added a variable in our Post.java file to store the featured Image Url like below.

 

String postImg;
public String getPostImg() {
return postImg;
}
public void setPostImg(String postImg) {
this.postImg = postImg;
}


 

For retrieving the featured image first we need to get the Url of the featured JSON Url and then we need to retrieve that json

 

JSONObject featureImage=obj.getJSONObject("_links");
JSONArray featureImageUrl=featureImage.getJSONArray("wp:featuredmedia");
JSONObject featureImageObj=featureImageUrl.getJSONObject(0);
String fiurl=featureImageObj.getString("href");


 

Retrieve the JSON and store the URL into the respective position in list.



 

if(fiurl!=null) {
Log.d(TAG, featureImageObj.getString("href"));
final int id=i;

JsonObjectRequest getMedia = new JsonObjectRequest(Request.Method.GET,
fiurl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, response.getString("source_url"));
post.setPostImg(response.getString("source_url"));
} catch (JSONException e) {
e.printStackTrace();
}

postAdapter.notifyItemChanged(id);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
}

);
requestQueue.add(getMedia);
}


 

After the above change we are successfully get the URL of our featured image and we need to set that into our imageview by modifying the our post adapter class as given below.

 

GlideApp.with(context)
.load(post.getPostImg())
.thumbnail(0.2f)
.apply(fitCenterTransform())
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.skipMemoryCache(false)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

Log.e(TAG, "Load failed", e);
for (Throwable t : e.getRootCauses()) {
Log.e(TAG, "Caused by", t);
}
e.logRootCauses(TAG);
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(((VideoViewHolder) holder).vImage);


 

Now our WP android app will look like as given below.

WP android app

If you have not read the first post of creating the WP Android App then you can first have look on below WP android app using REST and Volley.

https://www.nplix.com/2017/11/12/wp-android-app-using-rest/

 

Comments

  1. […] Next PostWP Android App using REST and volley part2 […]

    ReplyDelete
  2. Full Source Code Please

    ReplyDelete
  3. Hi Rakha,

    you can find the source code at below link.
    https://github.com/debugandroid/WPApp

    Let me know in case of any further issue.

    Thanks,

    ReplyDelete
  4. Thanks a lot bro Pawan Kumar.
    Have a great day!

    ReplyDelete
  5. How to get featured image of post using retrofit??

    ReplyDelete
  6. In reply to Sagar Sharma.
    Refer the below tutorial:-
    https://www.nplix.com/wp-android-app-using-rest-volley-livedata-part3/

    // getting URL of the Post fetured Image
    JSONObject featureImage=obj.getJSONObject(“_links”);
    JSONArray featureImageUrl=featureImage.getJSONArray(“wp:featuredmedia”);
    JSONObject featureImageObj=featureImageUrl.getJSONObject(0);
    String fiurl=featureImageObj.getString(“href”);

    ReplyDelete
  7. You wrote this post with great care and attention to detail regarding this issue. Your article provided me with useful information. It's quite beneficial to me as well as others. Mobile App Development Company In Melbourne

    ReplyDelete
  8. Nice job, this is essential information that is shared by you. This information is meaningful and factual for us to increase our knowledge about it. Always keep sharing this type of information. Website Design Ireland

    ReplyDelete
  9. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.Hire a hacker site.

    ReplyDelete
  10. Lots of valuable data can be taken from your article. I am happy that you have shared great info with us, It is a gainful article for us. Thankful to you for sharing an article like this. Android App Development Online Dubai

    ReplyDelete
  11. Thanks for publishing such great knowledge. You are doing such a great job. This info is very helpful for everyone. Keep it up. Thanks once again for sharing it. hire hackers for whatsapp

    ReplyDelete
  12. check this kind of article and I found your article which is related to my interest.Coding Courses in Adelaide Genuinely it is good and instructive information. Thankful to you for sharing an article like this.

    ReplyDelete
  13. You are providing good knowledge. It is really helpful and factual information for us and everyone to increase knowledge. about Webpage Design .Continue sharing your data. Thank you.

    ReplyDelete
  14. I read your post and got it quite informative. I couldn't find any knowledge on this matter prior to. I would like to thanks for sharing this article here.web design company san antonio site.

    ReplyDelete
  15. Yes, Really very good information, this information is excellent and needy for everyone. I am very thankful to you for providing this kind of knowledge. Thanks once again for sharing it. John Deere Tractor price

    ReplyDelete
  16. I never found such type of blog. You blog is very different from other ones. Ashok Leyland 10 Wheeler

    ReplyDelete
  17. Your blog is very good, it will be easy to buy trucks.
    Tata commercial vehicle

    ReplyDelete
    Replies
    1. consider exploring Liberating Solution for additional insights and assistance. Thanks once again for sharing."

      Delete

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Create Custom EditText View in Android

We use the EditText for taking the input from the user and use it at several places in our project. We required to do lots of customization for each time and there are lots of redundant code we write. Writing and managing these redundant codes is very difficult for example if we want to change the look and feel of the view them we need to modify it at each place where our EditText is getting used. So to avoid these kinds of the problem we can create our own Custom EditText View by just. The EditText view is just an extension of the TextView with lots of editing option and properties that required for the user input. How To Create Custom EditText View For creating the Custom EditText we need to extend the AppCompatEditText and override all three constructors of the view as given below. import android.content.Context; import android.graphics.Typeface; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; public