2014-02-27 38 views
0

我創建了一個應用程序,用於在登錄到該網站後,使用jsoup從網站中將表格導入到android中。雖然它運行良好,但速度很慢,需要大量時間才能啓動。任何人都可以幫助我解決這個問題。我附上下面的代碼,並在此先感謝加速使用Jsoup將網頁加載到android中

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     tv=(TextView)findViewById(R.id.tv); 
     Bundle extra=getIntent().getExtras(); 
     if(extra!=null){ 
      Log.v("got", extra.getString("user")+extra.getString("pass")); 
      user=extra.getString("user"); 
      pass=extra.getString("pass"); 
     } 
     HomeList homelist=new HomeList(this); 


     try { 
      ArrayList<String> result=homelist.execute(user+pass).get(); 
      if(result.contains("rejected")){ 
       Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
      else{ 
       tv.setText(user.toUpperCase(Locale.US)); 
       adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result); 
       lv=(ListView)findViewById(R.id.list); 
       lv.setAdapter(adapter); 

      } 

     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

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


    } 
    public class HomeList extends AsyncTask<String,String,ArrayList<String>> { 

     private List<String> cookies; 
     private HttpURLConnection conn; 
     private final String USER_AGENT="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"; 
     private Home home; 
     List<String> cook; 
     private int BUFF=50; 
     String username; 



      public HomeList(Home home) { 
      // TODO Auto-generated constructor stub 
       this.home=home; 
     } 


      public HomeList() { 
       // TODO Auto-generated constructor stub 
      } 


      @Override 


      protected ArrayList<String> doInBackground(String... arg0) { 
        // TODO Auto-generated method stub 
        publishProgress("Loading"); 
        String user=arg0[0].substring(0,10); 
        Log.v("user", user); 
        String pass=arg0[0].substring(10,arg0[0].length()); 
        Log.v("pass", pass); 
        //int code=Integer.parseInt(arg0[0].substring(arg0[0].length()-1)); 
        ArrayList<String> no=new ArrayList<String>(); 
        no.add("rejected"); 

        String url = "http://borealis.astra.edu.in"; 
        //action url in form 
        String astra="http://borealis.astra.edu.in/index.php"; 
        String attendance="http://borealis.astra.edu.in/index.php?option=com_base_attendancereport&Itemid=98"; 
        HomeList coll=new HomeList(); 
        //for cookies 
        CookieHandler.setDefault(new CookieManager()); 
        //get form data to be sent 
        String page=coll.GetPageContent(url); 
        //collecting form data 

        String postParams = coll.getFormParams(page,user,pass); 
        System.out.println(postParams); 
        cook=cookies; 
        System.out.print(cook); 
        ArrayList<String> result=coll.sendPost(url,postParams); 
        if(result.contains("error")) 
         return no; 
        else 
        return result; 
        } 


      private ArrayList<String> sendPost(String url, String postParams) { 
       // TODO Auto-generated method stub 

       URL obj; 
       try { 
        obj = new URL(url); 

       conn=(HttpURLConnection)obj.openConnection(); 
       conn.setInstanceFollowRedirects(false); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Host", "borealis.astra.edu.in"); 
       conn.setRequestProperty("User-Agent", USER_AGENT); 
       conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
       conn.setRequestProperty("Accept-Language","en-US,en;q=0.8"); 
       for(String cookie:this.cookies){ 
        conn.setRequestProperty("cookie", cookie.split(";",1)[0]); 
        System.out.println(cookie); 
       } 
       conn.setRequestProperty("Connection","keep-alive"); 
       conn.setDoOutput(true); 
       conn.setDoInput(true); 
       DataOutputStream wr=new DataOutputStream(conn.getOutputStream()); 
       wr.writeBytes(postParams); 
       wr.flush(); 
       wr.close(); 
       int responseCode=conn.getResponseCode(); 
       System.out.println(conn.getURL()); 
       System.out.println("posting data to "+url); 
       System.out.println("parametrs are "+postParams); 
       System.out.println("response Code "+responseCode); 
       URL secondURL = new URL(conn.getHeaderField("Location")); 
       conn=(HttpURLConnection) secondURL.openConnection(); 
       BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()),BUFF); 
       Log.d("length", "got"+conn.getContentLength()); 
       System.out.println("redirected url" + conn.getURL()); 
       String inputLine; 
       StringBuffer response=new StringBuffer(); 
       while((inputLine=in.readLine())!=null){ 
        response.append(inputLine); 
        } 
       Document doc=Jsoup.parse(response.toString()); 

       Elements table=doc.getElementsByClass("tiles_box"); 
       in.close(); 
       ArrayList<String> ele=new ArrayList<String>(); 
       if(table.first()!=null){ 

        Iterator<Element> ite=table.select("tr").select("td").iterator(); 
        ele.add(ite.next().text()); 
        Log.d("td", ele.toString()); 
        while(ite.hasNext()){ 
         String td=ite.next().text(); 
         if(!ele.contains(td)){ 
          ele.add(td); 
          Log.d("td", ele.toString()); 
          } 
        } 
        return ele; 
       } 

      else 
      { 
       ArrayList<String> error=new ArrayList<String>(); 
       error.add("error"); 
       return error; 

      } 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       return null; 
      } 




      private String getFormParams(String html, String user, String pass){ 
       // TODO Auto-generated method stub 
       try{ 

       System.out.println("extracting form data"); 
       Document doc=Jsoup.parse(html); 
       Element form=doc.getElementById("form-login"); 
       Elements inputElements=form.getElementsByTag("input"); 
       List<String> paramsList=new ArrayList<String>(); 
       for(Element inputElement:inputElements){ 
        String key=inputElement.attr("name"); 
        String value=inputElement.attr("value"); 
        if(key.equals("username")) 
         value=user; 
         if(key.equals("passwd")) 
          value=pass; 
          paramsList.add(key+"="+URLEncoder.encode(value,"UTF-8")); 
       } 
         StringBuilder result=new StringBuilder(); 
         for(String param:paramsList){ 
          if(result.length()==0){ 
           result.append(param); 
          }else{ 
           result.append("&"+param); 
          } 
         } 
         return result.toString(); 
       }catch(Exception e){ 
        Log.d("error", e.toString()); 
       } 
       return null; 

      } 

      private String GetPageContent(String url){ 
       // TODO Auto-generated method stub 
       try{ 
       URL obj=new URL(url); 

       conn=(HttpURLConnection)obj.openConnection(); 
       conn.setChunkedStreamingMode(20); 
       conn.setRequestMethod("GET"); 
       conn.setUseCaches(true); 
       conn.setRequestProperty("Host", "borealis.astra.edu.in"); 
       conn.setRequestProperty("User-Agent", USER_AGENT); 
       conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
       conn.setRequestProperty("Accept-Language","en-US,en;q=0.8"); 

       if(cookies!=null) 
        { 
        for(String cookie:this.cookies){ 

        conn.addRequestProperty("cookie", cookie.split(";",1)[0]); 
       } 
        } 
       int responseCode=conn.getResponseCode(); 
       System.out.println("sending get request "+url); 
       System.out.println("response code is "+responseCode); 
       BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()),BUFF); 
       String inputLine; 
       StringBuffer response=new StringBuffer(); 
       while((inputLine=in.readLine())!=null){ 



        response.append(inputLine); 


       } 
       in.close(); 
       setCookies(conn.getHeaderFields().get("Set-Cookie")); 
       return response.toString(); 
       }catch(Exception e){ 
        Log.d("error", e.toString()); 
       } 
       return null; 
      } 

      private void setCookies(List<String> cookies) { 
       // TODO Auto-generated method stub 
       this.cookies=cookies; 
       cook=cookies; 
       System.out.print(cook); 

      } 
      public List<String> getCookies(){ 
       return cookies; 
      } 




      @Override 
      protected void onProgressUpdate(String... values) { 
       // TODO Auto-generated method stub 
       super.onProgressUpdate(values); 
       Toast.makeText(getApplicationContext(), values[0], Toast.LENGTH_LONG).show(); 
      } 
      @Override 
      protected void onPostExecute(ArrayList<String> result) { 
       // TODO Auto-generated method stub 
       super.onPostExecute(result); 

      } 
    } 

回答

0

這一行ArrayList<String> result=homelist.execute(user+pass).get();你失去多線程的所有優點。問題是get()方法,其中

等待如果必要的計算完成,然後檢索其結果。

在我看來,你應該使用某種回調,這樣的:

public class MyAsyncTask extends AsyncTask<String, Void, String> { 

    public interface OnWorkDone { 
     public void doSomething(String s); 
    } 

    private OnWorkDone listener; 

    public void setListener(OnWorkDone listener) { 
     this.listener = listener; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String result = null; 
     //do something 
     return result; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (listener != null) { 
     listener.doSomething(result); 
     } 
    } 
} 

而且,在主類:

MyAsyncTask task = new MyAsyncTask(); 
task.setListener(new OnWorkDone() { 

    @Override 
    public void doSomething(String s) { 
      //Use results 
    } 
}); 

task.execute(params); 
+0

我們可以使用List作爲doSomething的的說法? ? –

+0

是的,你可以使用任何你想要的!這是一種像其他人一樣的方法.. – user2340612

+0

你說的get()部分是正確的,它會讓應用程序變得更慢。雖然我沒有使用接口部分,但我可以通過刪除get()來使其工作。謝謝:D –