2011-09-29 74 views
0

我正在开发Android 2.2上的应用程序。它主要做的是从互联网上获取数据,然后将数据显示在设备的屏幕上。我已完成一项活动,但速度太慢,获取网络内容需要太多时间。我想让它跑得更快。我想Async Task将是我应该使用的最合适的方法。我试图改变代码,插入Async Task,但我不知道如何做到这一点。 这是我的代码:Android应用程序加载问题

package com.nextlogic.golfnews; 



import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.text.Html; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class Activity1 extends Activity { 

    View vw; 
    ImageView im; 
    Bitmap bmp; 
    URL url = null; 


    //ImageView t; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     String s=""; 
     String pag=""; 
     String aux1=""; 


     ArrayList<String> pg; 
     ArrayList<String> links; 
     ArrayList<String> sbt; 
     TableLayout tableView = (TableLayout) findViewById(R.id.tableView); 
     String q=""; 
     String tt=""; 
     pag=getPage(); 
     aux1=pag.substring(pag.indexOf("\">Annonse<"),pag.indexOf("<!-- START articleListBullets -->"));   
     pg=title(aux1); 
     links=urls(aux1); 
     sbt=subtitle(aux1); 

     /////////////////////////////////////////////////////////////////////////// 
     for(int i=0; i<pg.size(); i++) { 

      s = "http://www.golfnews.no/" +links.get(i); 

      q=pg.get(i); 
      tt=sbt.get(i); 




       // create a new TableRow 
      TableRow row = new TableRow(this); 
      row.setBackgroundColor(Color.WHITE); 
      row.setTag(i); 
      row.setClickable(true); 
      row.setClickable(true); 
      row.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 

         Intent myIntent = new Intent(view.getContext(), News.class); 

         startActivityForResult(myIntent, 0); 
       } 
      }); 


      // create a new TextView 

      TextView fin = new TextView(this); 
      vw = new View(this); 
      im = new ImageView(this); 
      //////////////////////////////////// 

      bmp=getbmp(s); 
      im.setImageBitmap(bmp); 
      vw.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
      vw.setBackgroundColor(Color.LTGRAY); 

      row.addView(im, new TableRow.LayoutParams(70,30)); 


      q=titleEdit(q); 
      tt=subtitleEdit(tt); 




      ////////////////////////////////////// 
      fin.setText(Html.fromHtml("<b>" + q + "</b>" + "<br />" + 
        "<small>" + tt + "</small>")); 
      row.addView(fin); 
      tableView.addView(vw); 
      tableView.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

     } 
     } 

    public Bitmap getbmp(String s) 
    { 
     Bitmap bmp = null; 
     try{ 
      url = new URL(s); 
      } 
      catch(MalformedURLException e) 
      { 
        e.printStackTrace(); 
      } 
      try{ 
        HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
        conn.setDoInput(true); 
        conn.connect(); 
        InputStream is = conn.getInputStream(); 
        bmp = BitmapFactory.decodeStream(is); 

        } catch (IOException e) 
        { 
          e.printStackTrace(); 
        } 
      return bmp; 
    } 

    public String titleEdit(String q) 
    { 
     if(q.length()>33) 
     { 
      String q1 = q.substring(0,33); 
      String q2 = q.substring(33); 
      q =q1 + "<br />" +q2; 
     } 
     return q; 
    } 
    public String subtitleEdit(String tt) 
    { 
     if(tt.length()>40) 
     { 
      String tt1 = tt.substring(0,40); 
      String tt2 = tt.substring(40); 
      if(tt2.length()>42) 
      { 
       String z1 = tt2.substring(0,40); 
       String z2 = tt2.substring(40); 
       if(z2.length()>42) 
       { 
        String z21 = z2.substring(0,40); 
        String z22 = z2.substring(40); 
        z2=z21+"<br />"+z22; 
       } 
       tt2=z1+"<br />"+z2; 
      } 
      tt = "<br />"+tt1 + "<br />" +tt2; 
     } 

     return tt; 
    } 
    private ArrayList<String> title (String trax) 
    { 

     ArrayList<String> result= new ArrayList<String>(); 
     int ok=1; 
     int s1,s2; 
     while(ok==1) 
     { 
      //System.out.println("INDEX = "+trax.indexOf("alt=")); 
      ok=0; 
      if((trax.indexOf("alt=")!=-1&&trax.indexOf("\"/>")!=-1)&&((trax.indexOf("alt=")<trax.indexOf("\"/>")))) 
      { 


      ok=1; 
      s1 =trax.indexOf("alt="); 
      s2 = trax.indexOf("\"/>"); 
     //System.out.println("s1= "+s1+" s2 = "+s2+" length="+trax.length()); 
      result.add(trax.substring(s1+5,s2)); 
     // i++; 
      trax = trax.substring(s2 + 3); 

      } 

     } 
     return result; 
    } 
    private ArrayList<String> subtitle (String trax) 
    { 

     ArrayList<String> result= new ArrayList<String>(); 
     int ok=1; 
     int s1,s2; 
     while(ok==1) 
     { 

      ok=0; 
      if(trax.indexOf("<p>")!=-1) 
      { 


      ok=1; 
      s1 =trax.indexOf("<p>"); 
      s2 = trax.indexOf(")"); 
      //System.out.println("s1= "+s1+" s2 = "+s2+" length="+trax.length()); 
      result.add(trax.substring(s1+3,s2+1)); 
      trax = trax.substring(s2+1); 

      } 

     } 
     return result; 
    } 
    private ArrayList<String> urls (String trax) 
    { 

     ArrayList<String> result= new ArrayList<String>(); 
     int ok=1; 
     int s1,s2; 
     while(ok==1) 
     { 
      //System.out.println("INDEX = "+trax.indexOf("alt=")); 
      ok=0; 
      if((trax.indexOf("<img src")!=-1&&trax.indexOf("alt=\"")!=-1)&&((trax.indexOf("<img src")<trax.indexOf("alt=\"")))) 
      { 


      ok=1; 
      s1 =trax.indexOf("<img src"); 
      s2 = trax.indexOf("alt=\""); 
     // System.out.println("s1= "+s1+" s2 = "+s2+" length="+trax.length()); 
      result.add(trax.substring(s1+10,s2-2)); 
     // i++; 
      trax = trax.substring(s2 + 6); 

      } 

     } 
     return result; 
    } 
     private String getPage() { 
      String str = "***"; 

      try 
      { 
       HttpClient hc = new DefaultHttpClient(); 
       HttpPost post = new HttpPost("http://golfnews.no/nyheter.php"); 
       HttpResponse rp = hc.execute(post); 

       if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
       { 
        str = EntityUtils.toString(rp.getEntity()); 
       } 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 

      return str; 
     } 


} 

的活动需要太多的时间来加载(约10秒)。所以,请你能帮我弄清楚在哪里以及如何插入Async Task?或者,也许你可以建议我一些其他的东西来让应用程序运行得更快?例如像一个处理程序?因为我是Android编程的新手,所以我不太了解。任何意见将不胜感激。谢谢。

回答

0

1)你必须提高你的代码风格 2)对于这样的目的,你必须使用AsynchTask或处理器+独立的线程 12

+0

呀,非常感谢教我如何使用谷歌。我只是不知道如果没有你的帮助我会怎么做。你应该得到一个奖章或这个职位的东西。 – Teo