HttpClient:

今天实战下httpclient请求网络json数据,解析json数据返回信息,显示在textview,

起因:学校查询饭卡余额,每次都要访问校园网(内网),才可以查询,然后才是登录学校查询饭卡的网站查,

有木有很麻烦,今天我来测试下,正好学到httpclient请求网络数据,用httpclient来请求数据,查询饭卡余额

1:先是用fiddler截包,获取网络请求参数,第一步先是要用Student账号登录校园网,通过分析得到post请求用户名没有加密,密码是加密后的密码

POST http://ip/ HTTP/1.1
Host: ip
Connection: keep-alive
Content-Length:
Cache-Control: max-age=
Origin: http://ip
Upgrade-Insecure-Requests:
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://ip/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8 DDDDD=学号&upass=加密后的字符串&R1=0&R2=1&para=00&0MKKey=打码

2:第二步是访问饭卡查询网,截包分析这里,XSP000学号,分析得到获取json数据只要下面的GET请求就可以了,

GET http://ip2/web/SystemListener?className=cn.com.system.query.DealQuery&methodName=getDealInfo&paramCount=1&param_0=XSP000学号&_dc=1482242227206 HTTP/1.1
Host: ip2
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://ip2/web/main.jsp
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: JSESSIONID=xxxxxxxxxxxxxxxxxxx; JSESSIONID=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

返回的json信息

[
{"status":"0","name":"用户编号","info":"打码"},
{"status":"0","name":"用户姓名","info":"打码"},
{"status":"0","name":"帐号信息","info":"打码"},
{"status":"0","name":"卡号","info":"打码"},
{"status":"0","name":"所属机构","info":"打码"},
{"status":"1","name":"卡状态","info":"正常"},
{"status":"0","name":"用户类型","info":"学生"},
{"status":"0","name":"账户金额","info":"28元"},
{"status":"0","name":"脱机消费总额","info":"0元"},
{"status":"0","name":"登录日期","info":"2016年12月20日 星期二"}
]

第三步:实战操作,

3.1界面布局

3.2 在手机端用学生账号连接校园网,当打开这个程序的时候,程序自动登录校园网,程序是先执行OnCreate加载界面这时界面还没有呈现给用户,在Onstart()函数执行完数据初始化,这时就要post登录校园网写在OnStart()里面初始化数据,执行完onStart()函数后,界面才程序给用户,当成功登录后,登录手机端程序的时候就弹窗成功登录

(根据登录校园网成功后返回  <title>登录成功</title>信息)

Onstart里面的主要代码:

protected void onStart() {
super.onStart();
final Handler handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == ) {
//登录成功弹窗:成功登录,失败:提示返回信息
Toast.makeText(getApplicationContext(), msg.getData().getString("info"), Toast.LENGTH_SHORT).show();
}
}
}; new Thread(new Runnable() {
@Override
public void run() {
String result_login = LoginHttpUtil.requestNetForPost("学号", "密码加密后的字符串");
String regex = "<title>(.*?)</title>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(result_login);
while (m.find()) {
String str = m.group();
Message msg = Message.obtain();
Bundle data = new Bundle();
data.putString("info", str);
msg.setData(data);
msg.what = ;
handler1.sendMessage(msg);
}
}
}).start();
}

3.3 然后输入用户和密码,这里密码可用不用输入,GET请求里面没有用到password,可怕,学校太渣了-><-

在onCreate里面按钮点击事件写一个登录饭卡网函数

//饭卡查询结果
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == ) {
String name = msg.getData().getString("name");
String yue = msg.getData().getString("yue");
String institution = msg.getData().getString("institution");
String datetime = msg.getData().getString("datetime");
ca_showname.setText(name);
ca_showmoney.setText(yue);
ca_institution.setText(institution);
ca_time.setText(datetime);
}
}
}; public void loginCard() {
final String username = ed_ca_username.getText().toString();
final String password = ed_ca_password.getText().toString();
//判断是否密码或者用户名为空
if (TextUtils.isEmpty(username)) {
Toast.makeText(getApplication(), "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}
//post方法登录饭卡查询网是否成功
LoginHttpUtil.requestNetFromMealCard(handler, username);
// System.out.println("================是否登录学校网:"+result);
}

新建一个包,里面新建一个类,写网络请求的操作

public class LoginHttpUtil {

    //登录校园网的操作
public static String requestNetForPost(final String username, final String password) { // 根据url获得HttpPost对象
HttpPost httpRequest = new HttpPost("http://ip1");
// 取得默认的HttpClient
DefaultHttpClient httpclient = new DefaultHttpClient();
String strResult = null;
// NameValuePair实现请求参数的封装
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("DDDDD", username));
params.add(new BasicNameValuePair("upass", password));
params.add(new BasicNameValuePair("R1", ""));
params.add(new BasicNameValuePair("R2", ""));
params.add(new BasicNameValuePair("para", ""));
params.add(new BasicNameValuePair("0MKKey", "打码")); httpRequest.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpRequest.addHeader("Origin", "ip1");
httpRequest.addHeader("Referer", "ip1");
httpRequest.addHeader("Upgrade-Insecure-Requests", "");
httpRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); try {
// 添加请求参数到请求对象
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 获得响应对象
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 判断是否请求成功
if (httpResponse.getStatusLine().getStatusCode() == ) {
//转换为gb2312
strResult = EntityUtils.toString(httpResponse.getEntity(), "gb2312");
} else {
strResult = "错误响应:" + httpResponse.getStatusLine().toString();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
} //get方式登录饭卡网
public static void requestNetFromMealCard(final Handler handler, final String username) { new Thread(new Runnable() {
@Override
public void run() {
String url="http://ip2/SystemListener?className=cn.com.system.query.DealQuery&methodName=getDealInfo&paramCount=1&param_0=XSP000"+username;
//httpGet对象
HttpGet httpGet = new HttpGet(url);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
String JsonResult=null;
try {
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==){
//返回的信息转为json字符串数组
JsonResult=EntityUtils.toString(httpResponse.getEntity());
JSONArray array=new JSONArray(JsonResult);
//System.out.println("=============饭卡信息:"+array); String yue = array.getString();//根据索引得到索引位置7的余额,索引从0开始
JSONObject yue1=new JSONObject(yue);
String yue2 = yue1.getString("info"); String name = array.getString();
JSONObject name1=new JSONObject(name);
String name2 = name1.getString("info");//用户姓名 String institution = array.getString(); //所属机构institution
JSONObject institution1=new JSONObject(institution);
String institution2 = institution1.getString("info"); String datetime = array.getString(); //登录日期
JSONObject datetime1=new JSONObject(datetime);
String datetime2 = datetime1.getString("info"); Message msg= Message.obtain();
msg.what=;
Bundle data = new Bundle();
data.putString("name",name2);
data.putString("yue",yue2);
data.putString("institution",institution2);
data.putString("datetime",datetime2);
// System.out.println("--------handler传递的值:"+data);
msg.setData(data);
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start(); }
}

收工,可怕,这个月饭卡只有这一点点了,我要去充值了

android 之httpclient方式提交数据的更多相关文章

  1. Android 采用post方式提交数据到服务器

    接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...

  2. Android 使用Post方式提交数据(登录)

    在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...

  3. Android 使用Post方式提交数据

    在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...

  4. Android 采用get方式提交数据到服务器

    首先搭建模拟web 服务器,新建动态web项目,servlet代码如下: package com.wuyudong.web; import java.io.IOException; import ja ...

  5. Android 使用HttpClient方式提交POST请求

    final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...

  6. Android 使用HttpClient方式提交GET请求

    public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...

  7. httpclient方式提交数据到服务器

    get方式: //使用HttpClient请求服务器将用户密码发送服务器验证 try{ String path = "http://192.168.13.83:8080/xuexi/serv ...

  8. android 通过post方式提交数据的最简便有效的方法

    public boolean post(String username, String password) throws Exception { username = URLEncoder.encod ...

  9. Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)

    1.前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善 ...

随机推荐

  1. 查看机器上安装的jdk能支持多大内存

    命令:java -Xmx1024m -version C:\Users\maite>java -Xmx1024m -version java version "1.8.0_31&quo ...

  2. bootstrap-table分页第二篇 延续上一篇的

    这个是service/** * 返回记录数 * @return */ public Integer getPageTotal(Integer userid); //service的实现类 public ...

  3. windows

    1.拷贝远程文件 net use \\10.130.80.62\ipc$ 密码 /user:用户名 xcopy "\\10.130.80.62\G$\yt\apache-tomcat-7.0 ...

  4. QuartZ2D __ 简单用法 1

    一. 简单做一个画板 1. 建立一个UIView类 2. 在.m里建立一个延展 3. 分别定义一个起点, 一个终点的结构体属性 . 在建立一个存储路径的数组 @interface DrawView ( ...

  5. 何为HDFS?

    该文来自百度百科,自我收藏. Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时, ...

  6. yii 简单依赖注入

    <?phpnamespace app\controllers;use Yii;use yii\di\Container;use yii\di\ServiceLocator;use yii\web ...

  7. **crontab的使用方式介绍和no crontab for root 提示的处理

    crontab的使用方式介绍   定时任务参数详解如下:  crontab -l     |  crontab -e    www.2cto.com   #*/30 * * * * ntpdate 1 ...

  8. Oracle事务

    Oracle事务的ACID特性 原子性Atomicity:事务中的所有动作要么都发生,要么都不发生. 一致性Consistency:事务将数据库从一种状态转变为下一种一致状态. 隔离性Isolatio ...

  9. Andriod学习笔记4:mac下搭建 Eclipse+CDT 集成开发环境

    下载CDT 从eclipse官网下载最新的Eclipse IDE for C/C++ Developers,例如eclipse-cpp-mars-1-macosx-cocoa-x86_64.tar.g ...

  10. Stack Overflow: The Architecture - 2016 Edition(Translation)

    原文: https://nickcraver.com/blog/2016/02/17/stack-overflow-the-architecture-2016-edition/ 作者:Nick Cra ...