在Android中下载网页的最快方式?

时间:2014-05-21 14:07:02

标签: java android android-asynctask

主要问题是一切正常。但它没有响应。显示结果需要时间。如何让它足够快以获得更快的结果。

单个活动中有三个AsyncTask ...一个用于autoTextview更改。每当用户输入一些文本时,它就会调用AsynTask函数。

AutoTextView textChangeListener ..当用户输入它所称的内容时

textView.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {

                seq = cs;

            }

            @Override
            public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                    int arg3)
            {

            }

            @Override
            public void afterTextChanged(Editable arg0)
            {
                new SearchTask().execute(seq.toString().trim());
            }
        });

autoTextview的AsyncTask是

private class SearchTask extends AsyncTask<CharSequence, Void, String> 
    {

        ArrayAdapter<String> adapter;

        String[] array;

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(CharSequence... params) 
        {
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(pak_url + params[0]);

                HttpResponse responce = httpclient.execute(httppost);

                HttpEntity httpEntity = responce.getEntity();

                String response = EntityUtils.toString(httpEntity);

                Log.d("response is", response);

                if (response != null) {
                    String substr = response.split("\\(")[2].split("\\)")[0];

                    array = substr.split(",");

                    for (int i = 0; i < array.length; i++) {

                        array[i] = array[i].replace("'", "");
                    }

                }

                return response;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if (array != null) {
                adapter = new ArrayAdapter<String>(PlayListActivity.this,
                        android.R.layout.simple_list_item_1, array);

                textView.setAdapter(adapter);

                adapter.getFilter().filter(seq);
            } 

第二个AsyncTask下载完整网页并将其存储在String ..以及收到字符串时。将autotextview文本附加到它并调用另一个AsyncTask ..一切顺利。但是不知道为什么会这么慢..有人指导我如何让它变得敏感。而且快..

class RetrieveFeedTask extends AsyncTask<String, Void, String> 
    {   
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();

            Utilities.hideSoftKeyboard(PlayListActivity.this);

            progressDialog = ProgressDialog.show(PlayListActivity.this,
                    "Loading...", "Please wait...");
        }

        @Override
        protected String doInBackground(String... urls) {
            try {
                URL url= new URL(urls[0]);

                URLConnection conn = url.openConnection();

                // open the stream and put it into BufferedReader
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));

                String inputLine;
                String HTML_response = "";  
                while ((inputLine = br.readLine()) != null) {
                    // System.out.println(inputLine);
                    HTML_response += inputLine;
                }
                br.close();

                System.out.println("Done");

                return HTML_response;
            } /*catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }*/catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(String feed)
        {
            super.onPostExecute(feed);

            Parser(feed);

            progressDialog.dismiss();
        }
    }

    @SuppressLint("NewApi")
    void Parser(String x) 
    {
        if (x.contains(spliter_start))
        {
            if (spliter_end.isEmpty())
            {
                x = x.substring(x.indexOf(spliter_start));
            }
            else
            {
                x = x.substring(x.indexOf(spliter_start),
                        x.indexOf(spliter_end));
            }

        }

        int i = 0;
        list.clear();
        while (x.contains(loop_controller) && i<50)
        {
            /*if (i > 50)
            {
                break;
            }*/

            HashMap<String, String> map = new HashMap<String, String>();

            x = x.substring(x.indexOf(song_start));


            map.put("songsName",x.substring(
                    x.indexOf(song_start) + song_start.length(),
                    x.indexOf(song_end)));// songsName.get(i));

            x = x.substring(x.indexOf(song_url_start));

            map.put("songsUrl",x.substring(
                    x.indexOf(song_url_start) + song_url_start.length(),
                    x.indexOf(song_url_end)));// songsUrl.get(i));


            list.add(map);

            i++;
        }

        PlayListAdapter adapter = new PlayListAdapter(
                PlayListActivity.this, list);

        list_of_songs.setAdapter(adapter);

    }

任何人都可以告诉我如何使它快速响应..或者告诉我一个比AsyncTask更好的方法..并且请告诉我如何使用线程下载网页。或者它是否更好或我使用其他方式..

AnyBody通过查看代码帮助我,并告诉我如何快速完成。有人告诉我使用HttpClient类而不是UrlConnection。哪一个更好。我正在做什么错误,因为我的应用程序运行缓慢。

提前致谢...

0 个答案:

没有答案