Android客户端与服务器端交互方式总结
2015-06-23 22:08
453 查看
一、HttpClient 方式:
//第一步:创建一个客户端实例 HttpClient httpClient = new DefaultHttpClient(); //第二步:创建Http请求 HttpPost httpRequest = new HttpPost(url); //第三步: Entity作为Http的request报文的一部分进行传输 httpRequest.setEntity(new UrlEncodedFormEntity( nameValuePair )); //第四步:发送http的请求request,并接受服务器返回的response HttpResponse response=httpClient.execute(httpRequest); //第五步: 根据返回状态码,若成功(200)则获取Entity,若失败,则返回 if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) result=EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); else result="登陆失败!";
二、HttpURLConnection方式
For example, to retrieve the webpage at http://www.android.com/:URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } }
相关文章推荐
- Android-SD卡的读写
- 安装Android时 sdk manager解决方案
- ubuntu 14.10 安装android NDK
- Google《Android性能优化》学习笔记
- Android Looper.class
- Android开发之Intent
- Android模拟多线程的断点下载
- Android通讯录数据库介绍与基本操作(增删改查)
- android的布局xml文件如何添加注释?
- Android开发:GridView与ListView相互切换(动画效果)
- Android中Fragment的知识
- Android OpenGL ES(三)----编程框架
- 安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?
- Android 图片裁剪 超出内存
- Android实战简易教程-第二十枪(GridView长按显示删除)
- android 伸缩控件ExpandableListView 展开失败的可能原因。
- android布局之 GridLayout的使用
- Activity的七大生命周期
- android-textview
- 通过Intent传输图片导致广播消息异常的问题根因分析(通过分析Android源码反向推理)