开发者

how to download data asynchrnouslly

Please see my code

  package com.morris.eventHandling;
  import java.io.BufferedReader;
 开发者_如何学C 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();


        }

} As above data is coming synchronously but i want to asynchronously please help how can I do this.


use AsyncTask to download data asynchronously.

You can also see the following link for more info.

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

The above link will give you different methods to achieve the something.


Look at Painless Threading and AsyncTask

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜