这个话题在这里已经讨论了很多,但是没有一个解决方案对我有用。我想替换从HTML获得的字符串的一部分。获取并显示HTML可以正常工作,但是我无法删除字符串的任何部分。它的作用是找不到它。

请看下面的代码:

public class Main extends Activity {

public static String URL = "";
public static String htmlString;
public TextView mainText;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

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

    constructURL();
    getHtml();

    htmlString.replaceAll("<head>", "hulo");

    mainText.setText(htmlString);
}

public void getHtml() {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(URL);
        HttpResponse response = httpClient.execute(httpGet, localContext);
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(
                    response.getEntity().getContent()
                    )
            );
        String line = null;
        while ((line = reader.readLine()) != null){
            htmlString += line + "\n";
        }
    } catch (Exception e) {
    }
}

public void constructURL() {
    Time time = new Time();
    time.setToNow();
    String year = convertToString(time.year - 2000);
    String month = convertToString(time.month + 1);
    String monthDay = convertToString(time.monthDay);

    URL = "http://www.gymzl.cz/bakalari/suplovani_st/tr" + year + month + monthDay + ".htm";
}

public String convertToString(int value) {
    String text = "";
    if(value < 10) text = "0";
    text += String.valueOf(value);
    return text;
}
}


“ hulo”替代似乎无效。

对于这么长的代码,我感到很抱歉,但是我已经尝试了一切。

最佳答案

replaceAll不会更新调用字符串,您需要将其分配回来。改变这个

htmlString.replaceAll("<head>", "hulo");




htmlString = htmlString.replaceAll("<head>", "hulo");

关于java - 更换琴弦零件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23057561/

10-10 18:39
查看更多