如何异步下载数据

时间:2011-09-25 16:12:58

标签: android asynchronous

请参阅我的代码

  package com.morris.eventHandling;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.StringReader;
  import java.net.URI;

  import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;

 import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;

 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.os.Bundle;

 import android.util.Base64;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
import android.widget.Toast;

 public class EventHandling extends Activity implements OnClickListener {
/** Called when the activity is first created. */

ProgressDialog dialog ;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  Button btn=(Button)findViewById(R.id.button1);
 btn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    try {
        executeHttpGet();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         Toast.makeText(this, e+"", Toast.LENGTH_LONG).show();
    }
}
 public void executeHttpGet() throws Exception {
        dialog = new ProgressDialog(this);
        dialog.setCancelable(true);

        // set a message text
        dialog.setMessage("Loading...");

        // show it
      //  dialog.show();
         BufferedReader in = null;

         DefaultHttpClient client = new DefaultHttpClient();

         String url=   "http://newdev.xxxxxxx.com/morris/interface/mobile.php?method=dealerLogin&username=xxxx&password=xxxxx";

         String login = "xxxxx";
         String pass = "xxxxx";
         HttpGet request = new HttpGet();
         client.getCredentialsProvider().setCredentials(new AuthScope("newdev.objectified.com", 80), new UsernamePasswordCredentials(login, pass));
         request.setURI(new URI(url));
         HttpResponse response = client.execute(request);

                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String page = sb.toString();
                DataHandler handler=new DataHandler();
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                xr.setContentHandler(handler);
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(page));           
                xr.parse(is);  
                UserData d=handler.getData();
                String data=d.sid+"\n"+d.dtitle+"\n"+d.dcountry+"\n"+d.dcity;
                Toast.makeText(this, data, Toast.LENGTH_LONG).show();
             //  dialog.dismiss();


        }

} 如上所述数据同步,但我想异步请帮助我如何做到这一点。

2 个答案:

答案 0 :(得分:0)

使用AsyncTask异步下载数据。

您还可以查看以下链接以获取更多信息。

http://developer.android.com/resources/articles/painless-threading.html

以上链接将为您提供不同的方法来实现这一目标。

答案 1 :(得分:0)