带有不需要的“\ n”的Android / Java字符串连接

时间:2012-12-26 23:54:41

标签: android string jsoup

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

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: .....

1 个答案:

答案 0 :(得分: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);
}