Android - 在后台打开URL

时间:2013-05-19 14:10:26

标签: android http url hyperlink

有一个网站例如http://website.com你可以喜欢那里的东西,例如这样的链接:http://website.com/3020/hype。您必须转到该链接才能获得文章(3020)。你只能喜欢插入的东西。

我有这个:

HttpClient client = new DefaultHttpClient();  
            String getURL = "http://website.com/3020/hype/";
            HttpGet get = new HttpGet(getURL);
            HttpResponse responseGet = client.execute(get, cookieStuff);

其中cookieStuff是我从loggin中获得的cookie字符串。

这有效,但需要很长时间。还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

是的,有一个更合适的选项..互联网连接和数据库连接应该在AyncTask或Handler中进行..这将改善连接所需的时间(连接将在后台进行)..

这是异步任务的示例:

private class YourTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... s) {

        //Here you have to make the loading / parsing tasks
        //Don't call any UI actions here. For example a Toast.show() this will couse Exceptions
        // UI stuff you have to make in onPostExecute method

    }

    @Override
    protected void onPreExecute() {
        // This method will called during doInBackground is in process
        // Here you can for example show a ProgressDialog
    }

    @Override
    protected void onPostExecute(Long result) {
        // onPostExecute is called when doInBackground finished
        // Here you can for example fill your Listview with the content loaded in doInBackground method

    }
     

}

执行AsyncTask:

  

new YourTask()。execute(“”);