Http请求
2016-05-12 20:12
661 查看
使用HC(HttpClient)/UC(HttpURLConnection)进行网络访问的基本步骤: 0. 申请权限 INTERNET访问权限 1. 任何网络访问的相关代码,必须在工 作线程中执行! 2. 创建HC/UC对象 3. 声明发起网络访问的方式(GET/POST) 4. 进行网络连接 5. 获得服务器响应的结果 6. 解析结果,提取需要的内容7. 解析结果要提交到UI线程进行呈现利用HttpClient的POST方式发起带参数的请求利用POST方式发起请求,参数要放到请求实体中,并且在请求头中添加对实体中参数的说明。添加说明:post.setHeader("Content-Type", "application/x-www-form-urlencoded");添加参数:List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("loginname", user.getName()));parameters.add(new BasicNameValuePair("password", user.getPassword()));parameters.add(new BasicNameValuePair("realname", user.getRealname()));parameters.add(new BasicNameValuePair("email", user.getEmail()));HttpEntity entity = new UrlEncodedFormEntity(parameters);post.setEntity(entity);
登录界面参数提交 1)POST方式发起登录请求 2)要提交三个参数,参数应该放在请求实体中: loginname:用户帐号 password:用户密码 code:验证码图片上的字符串(不区分大小写) 3) 要设置2个头: 第一个头用来描述请求实体中字符串 "Content-Type" "application/x-www-form-urlecoded" 第二个头用来提交sessionid "Cookie" sid 在HttpClient中设置头: post.setHeader(key,value); 在HttpURLConnection中设置头 connection.setReqeustProperty(key ,value); 显示员工列表界面:ShowActivity 添加员工信息界面:EmpActivity 添加员工信息接口: OnAddEmpFinishListener(){ void onAddFinish(String result); }
例:
//HttpURLConnection进行注册 new Thread(){ public void run() { try { URL url = new URL("http://192.168.220.1:8080/ems/regist.do"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true);//接收服务器响应的内容 connection.setDoOutput(true);//要向服务器提交内容 //在请求头中,为请求实体中的内容做说明 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.connect(); //客户端向服务器提交参数 OutputStream out = connection.getOutputStream(); PrintWriter pw = new PrintWriter(out, true); //String params = "loginname="+user.getName()+"&password="+user.getPassword()+"&realname="+user.getRealname()+"&email="+user.getEmail(); String params = getParams(user); pw.print(params); pw.close(); //客户端获取服务器的响应内容 InputStream in = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); final String result = br.readLine(); br.close(); //在主线程执行 new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { listener.onRegistFinish(result); } }); } catch (Exception e) { e.printStackTrace(); } }; }.start();
例:提交注册
new AsyncTask<Void, Void, String>(){ @Override protected String doInBackground(Void... params) { try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://192.168.220.1:8080/ems/login.do"); post.setHeader("Content-Type","application/x-www-form-urlencoded"); post.setHeader("Cookie",sid); List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("loginname", loginname)); parameters.add(new BasicNameValuePair("password", password)); parameters.add(new BasicNameValuePair("code", code)); HttpEntity entity = new UrlEncodedFormEntity(parameters); post.setEntity(entity); HttpResponse resp = client.execute(post); String result = EntityUtils.toString(resp.getEntity()); return result; } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(String result) { listener.onLoginFinish(result); }; }.execute();
相关文章推荐
- RobotFrameWork http/https oauth接口测试 (二)
- http协议,全部看懂面试就成功了一半
- 关于Android studio 启动报错Internal error. Please report to https://code.google.com/p/android/issues
- 支持https的axel修改
- Android中进行基于 HTTP协议的网络访问
- 使用HC(HttpClient)/UC(HttpURLConnection)进行网络访问的基本步骤:
- android的两种网络请求方式关键代码详解
- Android 服务器连接工具类HttpUtil
- http转https
- java中的网络通信
- 网络get/post请求异步同步请求回调
- 未能加载文件或程序集 system.Web.Http.WebHost解决办法。
- 机器学习之——神经网络模型
- RobotFrameWork http/https oauth接口测试 (一)
- Android开发本地及网络Mp3音乐播放器(十六)歌词显示及滚动事件实现、ViewPager使用
- Android开发本地及网络Mp3音乐播放器(十六)歌词显示及滚动事件实现、ViewPager使用
- Android中基于HTTP协议的网络访问的两种方式
- tcpdump讲解
- 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
- document.readyState和xmlhttp.onreadystatechange