I can only get part of a url (link) and put into string in android?

荒凉一梦 提交于 2019-11-30 10:01:53

问题


[Solved] - I need to resolve the problem below Ok I wasn't really sure how to word this question, but basically what I want to do is, I got a url from a RSS feed in android, and I need to put part of that url into a string, the url will look something like this: http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821' and I only want the part after id= ONLY THE ID NUMBER,Then I need the id to put it in the following url: http://shake.uprm.edu/~shake/archive/shake/**ID HERE**/download/tvmap.jpg, to load the image corresponding to the id in Glide: [Solved] This Part is solved but i have a other Problem

I have to ways to do this

First Way:

 //the original String
    String somestring = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
    //save the index of the string '=' since after that is were you find your number, remember to add one as the begin index is inclusive
    int beginIndex = somestring.indexOf("=") + 1;
    //if the number ends the string then save the length of the string as the end, you can change this index if that's not the case
    int endIndex = somestring.length();
    //Obtain the substring using the indexes you obtained (if the number ends the string you can ignore the second index, but i leave it here so you may use it if that's not the case)
    String theNumber = somestring.substring(beginIndex,endIndex);
    //printing the number for testing purposes
    System.out.println("The number is: " + theNumber);
    //Then create a new string with the data you want (I recommend using StringBuilder) with the first part of what you want
    StringBuilder sb=new StringBuilder("http://shake.uprm.edu/~shake/archive/shake/");
    // add the number
    sb.append(theNumber);
    //then the rest of the string
    sb.append("/download/tvmap.jpg");
    //Saving the String in a variable
    String endResult = sb.toString();
    //Verifying end result 
    System.out.println("The end result is: "+endResult);

Glide.with(context).load(endResult).into(holder.Thumbnail);

Second Way:

String url = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
    String[] array = url.split("id=");
    String id  = array[1];
    String urlToLoad = "http://shake.uprm.edu/~shake/archive/shake/"+id+"/download/tvmap.jpg"
    Glide.with(context).load(urlToLoad).into(holder.Thumbnail);

[Problem]

My problem is, if i put the URL normally, ie http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821 the two methods work for me, but if I get the url Via getLink() does not work for me. Please help me.
I hope I explained well. Thanks in advance
.

There is my Myadapter.java the method to get the link is current.getLink()

package com.example.rssreader;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.CardView;
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 com.bumptech.glide.Glide;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Efrain on 26-02-2016.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    ArrayList<FeedItem>feedItems;
    Context context;
    public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
        this.feedItems=feedItems;
        this.context=context;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
        FeedItem current=feedItems.get(position);
        holder.Title.setText(current.getTitle());
        holder.Description.setText(current.getDescription());
        holder.Date.setText(current.getPubDate());
        holder.Link.setText(current.getLink());
        //the original String
        String somestring = current.getLink();
        //save the index of the string '=' since after that is were you find your number, remember to add one as the begin index is inclusive
        int beginIndex = somestring.indexOf("=") + 1;
        //if the number ends the string then save the length of the string as the end, you can change this index if that's not the case
        int endIndex = somestring.length();
        //Obtain the substring using the indexes you obtained (if the number ends the string you can ignore the second index, but i leave it here so you may use it if that's not the case)
        String theNumber = somestring.substring(beginIndex,endIndex);
        //printing the number for testing purposes
        System.out.println("The number is: " + theNumber);
        //Then create a new string with the data you want (I recommend using StringBuilder) with the first part of what you want
        StringBuilder sb=new StringBuilder("http://shake.uprm.edu/~shake/archive/shake/");
        // add the number
        sb.append(theNumber);
        //then the rest of the string
        sb.append("/download/tvmap.jpg");
        //Saving the String in a variable
        String endResult = sb.toString();
        //Verifying end result
        System.out.println("The end result is: "+endResult);
        Glide.with(context).load(endResult).into(holder.Thumbnail);

    }



    @Override
    public int getItemCount() {
        return feedItems.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView Title,Description,Date,Link;
        ImageView Thumbnail;
        CardView cardView;
        public MyViewHolder(View itemView) {
            super(itemView);
            Title= (TextView) itemView.findViewById(R.id.title_text);
            Description= (TextView) itemView.findViewById(R.id.description_text);
            Date= (TextView) itemView.findViewById(R.id.date_text);
            Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
            cardView= (CardView) itemView.findViewById(R.id.cardview);
            Link= (TextView) itemView.findViewById(R.id.info);
        }
    }
}

回答1:


Do it like this

String url = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
String[] array = url.split("id=");
String id  = array[1];
String urlToLoad = "http://shake.uprm.edu/~shake/archive/shake/"+id+"/download/tvmap.jpg"
Glide.with(context).load(urlToLoad).into(holder.Thumbnail);

Then use this id where you want to use




回答2:


You can achieve that making a substring of the number you want to find and then appending that to the string you need.

The code would be as follows:

    //the original String
    String somestring = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
    //save the index of the string '=' since after that is were you find your number, remember to add one as the begin index is inclusive
    int beginIndex = somestring.indexOf("=") + 1;
    //if the number ends the string then save the length of the string as the end, you can change this index if that's not the case
    int endIndex = somestring.length();
    //Obtain the substring using the indexes you obtained (if the number ends the string you can ignore the second index, but i leave it here so you may use it if that's not the case)
    String theNumber = somestring.substring(beginIndex,endIndex);
    //printing the number for testing purposes
    System.out.println("The number is: " + theNumber);
    //Then create a new string with the data you want (I recommend using StringBuilder) with the first part of what you want
    StringBuilder sb=new StringBuilder("http://shake.uprm.edu/~shake/archive/shake/");
    // add the number
    sb.append(theNumber);
    //then the rest of the string
    sb.append("/download/tvmap.jpg");
    //Saving the String in a variable
    String endResult = sb.toString();
    //Verifying end result 
    System.out.println("The end result is: "+endResult);

After that you can put the String into Glide I hope It was clear and well explained.



来源:https://stackoverflow.com/questions/41068786/i-can-only-get-part-of-a-url-link-and-put-into-string-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!