您的位置:首页 > 理论基础 > 计算机网络

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();

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: