设为首页 收藏本站
查看: 941|回复: 0

[经验分享] Android的网络应用-使用Apache HttpClient

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-3 09:32:16 | 显示全部楼层 |阅读模式
  Android的网络应用-使用Apache HttpClient
  实例:访问被保护资源
  创建项目:HttpClientTest
  此项目要部署Web服务器,这里使用的Tomcat 7.0,在webApps目录下创建foo目录,并部署相应的文件
  即foo/secret.jsp,foo/login.jsp
   DSC0000.png
  项目运行结果:
  没有登录的情况下,访问被保护界面:
DSC0001.png
  
  登陆系统:
DSC0002.png
  
  
  登录成功后访问界面:
DSC0003.png
  
  main.xml










  


  login.xml












  



package org.wwj.net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class HttpClientTest extends Activity
{
Button get;
Button login;
EditText response;
HttpClient httpClient;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建DefaultHttpClient对象
httpClient = new DefaultHttpClient();
get = (Button) findViewById(R.id.get);
login = (Button) findViewById(R.id.login);
response = (EditText) findViewById(R.id.response);
get.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 创建一个HttpGet对象
HttpGet get = new HttpGet(
"http://183.30.191.50:8080/foo/secret.jsp");
try
{
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);
HttpEntity entity = httpResponse.getEntity();
if (entity != null)
{
// 读取服务器响应
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
String line = null;
response.setText("");
while ((line = br.readLine()) != null)
{
// 使用response文本框显示服务器响应
response.append(line + "\n");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
login.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
final View loginDialog = getLayoutInflater().inflate(
R.layout.login, null);
new AlertDialog.Builder(HttpClientTest.this)
.setTitle("登录系统")
.setView(loginDialog)
.setPositiveButton("登录",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
String name = ((EditText) loginDialog
.findViewById(R.id.name)).getText()
.toString();
String pass = ((EditText) loginDialog
.findViewById(R.id.pass)).getText()
.toString();
HttpPost post = new HttpPost(
"http://183.30.191.50:8080/foo/login.jsp");
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List params = new ArrayList();
params
.add(new BasicNameValuePair("name", name));
params
.add(new BasicNameValuePair("pass", pass));
try
{
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(
params, HTTP.UTF_8));
// 发送POST请求
HttpResponse response = httpClient
.execute(post);
// 如果服务器成功地返回响应
if (response.getStatusLine()
.getStatusCode() == 200)
{
String msg = EntityUtils
.toString(response.getEntity());
// 提示登录成功
Toast.makeText(HttpClientTest.this,
msg, 5000).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).setNegativeButton("取消", null).show();
}
});
}
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93539-1-1.html 上篇帖子: Android使用Apache的httpmime包post提交数据 下篇帖子: AWStats: Apache/IIS的日志分析工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表