2011-09-29 2 views
0

Je développe une application sur Android 2.2. Principalement ce qu'il fait, c'est qu'il saisit des données à partir d'Internet, puis il le montre sur l'écran de l'appareil. J'ai terminé une activité, mais c'est trop lent, il en faut trop pour obtenir le contenu Web. Je voudrais le faire courir plus vite. Je suppose que Async Task serait la méthode la plus appropriée que je devrais utiliser. J'ai essayé de changer le code, d'insérer le Async Task mais je n'arrive pas à comprendre comment le faire correctement. Ce est mon code.Application Android chargement numéro

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; 
     } 


} 

L'activité prend trop de temps à charger (environ 10 secondes). Alors s'il vous plaît, pouvez-vous m'aider à comprendre où et comment insérer le Async Task? Ou peut-être pouvez-vous me suggérer autre chose pour que l'application fonctionne plus vite? Quelque chose comme un gestionnaire par exemple? Je ne sais pas vraiment beaucoup puisque je suis nouveau à la programmation Android. Tout conseil serait très apprécié . Merci.

Répondre

0

1) Vous avez besoin d'améliorer votre style de code 2) Pour cette fin, vous devez utiliser AsynchTask ou gestionnaire + thread séparé 1, 2

+0

Oui, merci beaucoup pour moi enseigner comment utiliser Google. Je ne sais pas ce que j'aurais fait sans ton aide. Vous devriez obtenir une médaille ou quelque chose pour ce poste. – Teo