1

I'm developing an Android app. I'm using Jsoup to retreive elements from a page. Then, I'm iterating over the collection to get each individual part of it. I'm not sure how to save each instance of an element as a different variable. I think that I can use a for loop for this, but I don't quite understand it. How would I determine the length of how long to select from? How would I use it? I'm retreiving elements from here: http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380. My code is below:

public class TestStation extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.test_station);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        Intent intent = getIntent();
        String value = intent.getExtras().getString("value");

        Uri my = Uri.parse("http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid="+value);

        String myUrl = my.toString();



Document doc = null;

TextView tv1 = (TextView) findViewById(R.id.tv1);

try {
    doc = Jsoup.connect(myUrl).userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").get();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Elements elem = doc.select("eta"); 
Iterator<Element> iterator = elem.iterator();

while(iterator.hasNext())
{ Element div = iterator.next();

    Elements arrT = div.select("arrT");
     Elements prdt = div.select("prdt");
     Elements destNm = div.select("destNm");
    Elements rt = div.select("rt");
    String DestNm = destNm.text();
    String Rt = rt.text();
    tv1.setText(String.valueOf (Rt));

I would like to store each instance (there is many) of arrT, pdrt, and destNm as a different variable. How would I go across doing this? Thank you for your help.

Stephan
  • 41,764
  • 65
  • 238
  • 329
hichris123
  • 10,145
  • 15
  • 56
  • 70

3 Answers3

2

You could use a generic type list

ArrayList<Elements> xx = new ArrayList<Elements>();

then in your while loop

 xx.add(arrT);
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
0

`Could you make an class with getters and setters for the properties you're interested in, and create an array of that?

EDIT: To create your class:

public class YourClass {

private Element arrt;
...

public void setArrT (String input) {
    arrt = input;
}
public Element getArrt() {
 return arrt;
}
....

}

Give it a go.

Andrew
  • 8,445
  • 3
  • 28
  • 46
0

Transfer between Unicode and UTF-8

try { 
String string = "\u201cGoodbye Athens thanks";
byte[] utf8 = string.getBytes("UTF-8");
string = new String(utf8, "UTF-8");
}  catch (UnsupportedEncodingException e) {
}
Hao Lyu
  • 176
  • 1
  • 5