栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用AsyncTask进行android网络连接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用AsyncTask进行android网络连接

只是一个简单的例子,它看起来像:

public class IPControl extends Activity {    private ProgressDialog pd = null;    private String data = null;    private Socket socket;    private String serverIpAddress;    private static final int REDIRECTED_SERVERPORT = 32;    public PrintWriter out;    public BufferedReader in ;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        try{    this.pd = ProgressDialog.show(this, "Loading..", "Please Wait...", true, false); new AsyncAction().execute();        }catch (Exception e) { e.printStackTrace();        }    }    private class AsyncAction extends AsyncTask<String, Void, String> {        protected Void doInBackground(String... args) {  try {     InetAddress serverAddr = InetAddress.getByName(serverIpAddress);     socket = new Socket(serverAddr, REDIRECTED_SERVERPORT); } catch (UnknownHostException e1) {     e1.printStackTrace(); } catch (IOException e1) {     e1.printStackTrace(); } try {     out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));     while (! in.ready());     readBuffer();     out.println("rootrn");     //System.out.print("rootrn");     while (! in .ready());     readBuffer();     out.println("rootrn");     //System.out.print("rootrn");     while (! in .ready());     String msg = "";     while (in.ready()) {         msg = msg + (char) in .read();     } } catch (IOException e) {}        return null;//returns what you want to pass to the onPostExecute()    }    protected void onPostExecute(String result) {        //resultis the data returned from doInbackground        IPControl.this.data = result;        if (IPControl.this.pd != null) { IPControl.this.pd.dismiss();        }    } }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号