我试图创建一个应用程序,其中一个页面有它的文本自动创建通过阅读一个网站?
我理解数组将被使用和字符串格式化,我对android编程很好,但不是专家lol。
我曾尝试使用在这个网站上找到的一组编码,但我认为这只是用于在线读取文本文件:
private TextView outtext;
private String HTML;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*FROM HERE*/
outtext= (TextView) findViewById(R.id.textview1); //change id if needed!!!
try {
getHTML();
} catch (Exception e) {
e.printStackTrace();
}
outtext.setText("" + HTML);
/*TO HERE*/
}
private void getHTML() throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.bbc.co.uk/news/"); //URL!
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
result += line + "n";
HTML = result;
}
}
}
我试过使用bbc.co。例如Uk/news/。我的应用程序上的文本视图返回'null'。
希望有人能帮助我,提前感谢。
看起来像
HTML = result;
应该在while循环之外吗?
然后我怀疑HTML将在您设置TextView值时填充。您需要在线程中处理HTML请求,然后使用Handler将结果发布到UI:
处理程序类API
private final Handler handler = new Handler();
new Thread()
{
@Override
public void run()
{
getHTML();
handler.post( updateResults );
}
}.start();
final Runnable updateResults = new Runnable()
{
@Override
public void run()
{
updateUi();
}
};
private void updateUi()
{
outtext.setText( "" + HTML );
}