展会信息港展会大全

android Http请求
来源:互联网   发布日期:2015-09-28 16:23:33   浏览:1837次  

导读:需求:用户登录(name:用户名,pwd:密码)(一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultForHttpGet(String name,String pwd) throws ClientPr......

需求:用户登录(name:用户名,pwd:密码)

(一)HttpGet :doGet()方法

//doGet():将参数的键值对附加在url后面来传递

public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{

//服务器:服务器项目:servlet名称

String path="http://192.168.5.21:8080/test/test";

String uri=path+"?name="+name+"&pwd="+pwd;

//name:服务器端的用户名,pwd:服务器端的密码

//注意字符串连接时不能带空格

String result="";

HttpGet httpGet=new HttpGet(uri);

HttpResponse response=new DefaultHttpClient().execute(httpGet);

if(response.getStatusLine().getStatusCode()==200){

HttpEntity entity=response.getEntity();

result=EntityUtils.toString(entity, HTTP.UTF_8);

}

return result;

}

(二)HttpPost :doPost()方法

//doPost():将参数打包到http报头中传递

public String getReultForHttpPost(String name,String pwd) throws ClientProtocolException, IOException{

//服务器:服务器项目:servlet名称

String path="http://192.168.5.21:8080/test/test";

HttpPost httpPost=new HttpPost(path);

List<NameValuePair>list=new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair("name", name));

list.add(new BasicNameValuePair("pwd", pwd));

httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));

String result="";

HttpResponse response=new DefaultHttpClient().execute(httpPost);

if(response.getStatusLine().getStatusCode()==200){

HttpEntity entity=response.getEntity();

result=EntityUtils.toString(entity, HTTP.UTF_8);

}

return result;

}

赞助本站

人工智能实验室

相关热词: Http请求

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港