2012-12-26 33 views
1

我正在寫一個Android程序來解析來自Translink API網頁的數據。該程序的工作原理,但我與我正在存儲的數據有問題。似乎每次程序循環時,「\ n」都會添加到字符串本身。下面是代碼本身:Android/Java字符串與不需要的字節串「 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: ..... 
+0

然後刪除內部循環中的連接。 – nullpotent

+0

將該concat更改爲+ =並沒有真正做任何事情。 – user1927638

+0

而且您已驗證數據中沒有「\ n」? –

回答

0

你可以使用一個ArrayList,其中定義之外的所有for循環

// 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); 
} 
+0

我試過了,它似乎工作正常。最後,我發現我在使用substring()函數後在最後有一個額外的「\ n」。我結束了使用string = string.replace(「\ n」,「」);以使其正常工作。雖然謝謝! – user1927638

+0

@ user1927638請確保您將此標記爲正確的答案,只是爲了表明這已解決,最好的問候Myller – MAA

相關問題