2016-12-09 1 views
0

Ok, je n'étais pas vraiment sûr de savoir comment formuler cette question, mais fondamentalement ce que je veux faire est, j'ai reçu une URL d'un flux RSS dans android, et je dois mettre une partie de cette URL dans une chaîne, l'URL ressemblera à quelque chose comme ceci: http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821' et je ne veux que la partie après ID = SEULEMENT LE NUMÉRO ID, Ensuite, j'ai besoin de l'ID pour le mettre dans l'URL suivant: http://shake.uprm.edu/~shake/archive/shake/**ID HERE**/download/tvmap.jpg, pour charger l'image correspondant à l'ID dans Glide: [résolu] cette partie est résolu mais j'ai un autre problèmeJe peux seulement obtenir une partie d'une URL et mettre dans une chaîne?

Je dois façons de le faire

Première voie:

//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); 

Deuxième voie:

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); 

[Problème]

Mon problème est, si je mets l'URL normalement, c.-à-http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821 travaillent pour moi les deux méthodes, mais si je reçois l'url Via getLink() ne fonctionne pas pour moi. S'il vous plaît aidez-moi.
J'espère que j'ai bien expliqué. Merci d'avance
.

Il est mon Myadapter.java la méthode pour obtenir le lien est 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); 
     } 
    } 
} 

Répondre

0

Ceci est un cas parfait d'utilisation des expressions régulières:

Matcher matcher = Pattern.compile("id=(\\d*)") 
    .matcher("http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821"); 

if(matcher.matches()) 
    id = matcher.find().group(); 

Ici, le id=(\\d*) regex dit "find 'id =' dans une chaîne avec n'importe quel nombre de chiffres numériques après." La parenthèse (le groupe) nous permet de sortir les données spécifiques que vous cherchez, si le Matcher est capable de le trouver.

Et pour plus d'informations sur regex, vous devriez vérifier regex101

+0

Merci pour La réponse, mais j'ai la réponse à ce problème J'ai besoin de la réponse pour le dernier problème de ma question –

0

Essayez Uri.parse(url).getQueryParameter("id") .La fonction d'analyse syntaxique retourne un objet URI qui décompose un URI en parties, le getQueryParameter trouvailles paramètres de requête intégrée et retourne la valeur de chaîne. Si vous avez besoin de la valeur int, vous pouvez le convertir normalement.

+0

S'il vous plaît vous pouvez le faire pour moi, je suis une recrue –

+0

S'il vous plaît m'a donné le code merci d'avance –