1

我正在编写一个从 Translink API 网页解析数据的 Android 程序。该程序有效,但我存储的数据有问题。似乎每次程序循环时,都会在字符串本身中添加一个“\n”。这是代码本身:

private class DownloadWebpageText extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String...urls) {
        // params comes from the execute() call: params[0] is the url.
        try {
            // Test Code
            for(int h=0; h<prev_stops.size(); h++) {
                Document doc = Jsoup.connect("http://api.translink.ca/rttiapi/v1/stops/" + prev_stops.get(h) + "/estimates?&timeframe=120&apikey=XMy98rbwFPLcWWmNcKHc").get();
                nextbuses = doc.select("nextbus");
                schedules = doc.select("schedules");
                String bus_times = "";

                temp += "Arrival Times for Stop " + prev_stops.get(h) /*+ " (Previous Stop: " + prev_stop + "): \n" + "\nPrevious Stops: " + prev_stop*/;

                for(int i=0; i<nextbuses.size(); i++) {
                    temp += parseData(nextbuses.get(i).select("routeno").toString()) + ": ";

                    schedule = schedules.get(i).children();
                    expectedleavetime = schedule.select("expectedleavetime");

                    for(int j=0; j<expectedleavetime.size(); j++) {
                        if(j != 0) {
                            temp = temp.concat(", ");
                        }
                        temp = temp.concat(parseData(expectedleavetime.get(j).toString()));

                    }
                    temp += "\n";
                }
                temp += "\n";
            }

            return "";
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

parseData() 函数就是这样的:

public String parseData(String input) {
       int beg = input.indexOf(">") + 1;
       int end = input.lastIndexOf("<") - 1;
       return input.substring(beg, end);
     }

这是控制台的输出:

Arrival Times for Stop 56549
402: 
4:10pm, 
4:40pm, 
5:10pm, 
5:40pm

我希望输出是这样的:

Arrival Times for Stop 56549
402: 3:40pm, 4:10pm, 4:40pm, 5:10pm
403: .....
4

1 回答 1

0

您可以使用在所有 for 循环之外定义的 ArrayList

// Define our ArrayList
ArrayList<String> allStops = new ArrayList<String>();

// Define a string that holds information about the current stop
String currentStopInfo = "Arrival Times for Stop " + prev_stops.get(h);

for(int i=0; i<nextbuses.size(); i++) {

    // Define a temporary String that holds all the times for a specific bus
    String temp_2 = parseData(nextbuses.get(i).select("routeno").toString()) + ": ";

    schedule = schedules.get(i).children();
    expectedleavetime = schedule.select("expectedleavetime");

    for(int j=0; j<expectedleavetime.size(); j++) {
        if(j != 0) {
            temp_2 += ", ";
        }
        temp_2 = temp_2.concat(parseData(expectedleavetime.get(j).toString()));

     }

     // Add the string that holds info for one bus to our Array
     allStops.add(temp_2);
}

当您想打印所有字符串(每辆公共汽车的所有站点)(每个字符串都包含有关一辆公共汽车的信息)时,您只需执行以下操作:

//Print the line that holds the current stop information
System.out.println(currentStopInfo);
for(String x : allStops) {

     // Loop through our ArrayList, and print the information
     System.out.println(x);
}
于 2012-12-27T00:46:37.513 回答