0

I am designing an application in Kotlin which parses json-array. I have completed the application successfully but I was stuck on one minor(in my case major) glitch.I have successfully parsed the array and displayed the required array of objects in CardView and also opened the link in the browser when the card item is clicked.

Problem

The JSON contains certain links which are expired or are no longer in use. How do I handle such(expired) links from within the app when the user clicks on that particular card(with any flowery message or `ImageView)? I am comfortable in Android basics and still learning to devise Kotlin in android application and searched on the internet to the best of my knowledge but all my efforts went in vein.

Any help is warmly and benignly welcomed....

Below is my Code fragment(FeedViewHolder.kt):

class FeedViewHolder(itemView: View):RecyclerView.ViewHolder(itemView), View.OnClickListener,View.OnLongClickListener {

var txtTitle: TextView
var txtPubdate: TextView
var txtContent: TextView

private var itemClickListener: ItemClickListener? = null

init {
    txtTitle = itemView.findViewById(R.id.txtTitle) as TextView
    txtPubdate = itemView.findViewById(R.id.txtPubdate) as TextView
    txtContent = itemView.findViewById(R.id.txtContent) as TextView

    itemView.setOnClickListener(this)
    itemView.setOnLongClickListener(this)

}

fun setItemClickListener(itemClickListener: ItemClickListener) {
    this.itemClickListener = itemClickListener
}

override fun onClick(v: View?) {
    itemClickListener!!.onClick(v, adapterPosition, false)
}

override fun onLongClick(v: View?): Boolean {
    itemClickListener!!.onClick(v, adapterPosition, true)
    return true
}
}

Adapter Class

class FeedAdapter(private val rssObject: RSSObject, private  val 
 mContext:Context): RecyclerView.Adapter<FeedViewHolder>()
{
private val inflater:LayoutInflater

init {
    inflater = LayoutInflater.from(mContext)
}
override fun getItemCount(): Int {
    return rssObject.items.size
}

override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {

    holder.txtTitle.text = rssObject.items[position].title
    holder.txtContent.text = rssObject.items[position].Content
    holder.txtPubdate.text = rssObject.items[position].pubDate

    holder.setItemClickListener(ItemClickListener { view, position, isLongClick ->

        if(!isLongClick){

            val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.items[position].link))
            browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            mContext.startActivity(browserIntent)
        }

    })
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): FeedViewHolder {
val itemView = inflater.inflate(R.layout.row,parent,false)
    return FeedViewHolder(itemView)
}
  • I don't quite get what you mean with "expired links". Do you mean URLs that will take the user to some error page? Do you know if they are "expired" when the user clicks them? – fweigl Sep 13 '17 at 20:23
  • 2
    The problem is language agnostic surely, it is there whether you're using Kotlin or Java? I think you're asking how do I handle a Http Code 404, or something like that? Why not do a preliminary http request and check the response .. is 2xx and not 4xx, 5xx .. https://stackoverflow.com/a/18135030/4252352 – Mark Sep 13 '17 at 20:25
  • Sir(@Ascorbin) the links does not contain any thing means that the URL has been removed form the website? –  Sep 13 '17 at 20:28
  • @Mandy8055 Your best chance would then be to go with Mark Keens comment and "silently" check if the webpage / URL is still valid before starting your browser Intent. – fweigl Sep 13 '17 at 20:32
  • Sir(@Mark Keen) that is helpful I will surely look into it. The links are from news website so they keep on throwing **HTTP Code 404** on quick succession(probably 10 to 12 days).So for that also I have to keep a regular checks in the app itself? –  Sep 13 '17 at 20:32
  • Okay Sir(@Ascorbin) I will go on with it –  Sep 13 '17 at 20:34
  • 1
    The request will not be free, however should be a relatively inexpensive operation (depends on network speed / remote server speed etc..) - and if showing a expired/404 webpage has a negative impact on user experience, then I'd check each link every time if you can't guarantee the stability of the remote web sites. – Mark Sep 13 '17 at 20:37
  • Thank you Sir I will surely keep this in mind and try to implement it!!! –  Sep 13 '17 at 20:40

0 Answers0