显示网页内容

时间:2014-09-11 14:23:04

标签: java android eclipse web

基本上我要做的是在TextView中显示某些网页的网页内容。当我运行这个应用程序时,它不会崩溃,但它没有显示任何东西?我做错了什么?

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

  public void main(String[] args) throws Exception
  {

    URL oracle = new URL("http://www.pocketnow.com");
    BufferedReader in = new BufferedReader(
            new InputStreamReader(oracle.openStream()));

    String inputLine;
    TextView prikazTeksta = (TextView) findViewById(R.id.textView);
    while ((inputLine = in.readLine()) != null)
    {
      prikazTeksta.setText(inputLine.toString());
    }
    in.close();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
        // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings)
    {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

2 个答案:

答案 0 :(得分:0)

TextView.setText()替换所有内容,也许你想要TextView.append()

WebView可能更适合您的需求吗?

答案 1 :(得分:0)

Android中不存在

public void main 。它不会自动调用,您必须自己调用它。并且您无法在TextView中显示Web内容。这个工作有一个名为WebView的组件。只需删除 main ,用WebView替换XML中的TextView并使用以下代码:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.lyout.activity_main);
    WebView prikazTeksta = (WebView) findViewById(R.id.webView);
    prikazTeksta.loadUrl("http://www.pocketnow.com");
}

...