Android模拟登陆综合教务系统客户端(java)-jsoup解析数据

来源:互联网 发布:淘宝店铺绑定旺旺 编辑:程序博客网 时间:2024/09/21 08:57


现如今,基本的app都需要接入网络,下面介绍如何模拟登陆学校的教务系统获得里面的内容。


有Http基础的朋友都知道,我们是通过Get 与Post请求与服务器进行交互的,Get顾名思义就是获取信息,Post就是想服务器发请求,但是Post也可以用来获取信息并且比Get有很多优势,我们这里就是使用的Post。Java中有很多方式与服务器进行连接,常见的有HttpUrlCollection,HttpClient。两者的优缺点:

 需要用到的工具:httpwatch  (适用于火狐和IE浏览器)用于获得postdata.

1.如何获取验证码

private void DoGetVerifation() {        new Thread(new Runnable() {            @Override            public void run() {                HttpPost httPost = new HttpPost(VERIFATIONURL);                HttpClient client = new DefaultHttpClient();                try {                    HttpResponse httpResponse = client.execute(httPost);                    byte[] bytes = new byte[1024];                    bytes = EntityUtils.toByteArray(httpResponse.getEntity());                    bmVerifation = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);                } catch (IOException e) {                    e.printStackTrace();                }                if (bmVerifation == null) {                    Looper.prepare()                    Toast.makeText(MainActivity.this, "获取验证码失败请检查网络设置", Toast.LENGTH_SHORT).show();                    Looper.loop()                }                Message msg = new Message();                msg.arg1 = 10;                handler.sendMessage(msg);            }        }).start();    }

2.

post登陆信息

   <pre name="code" class="java"> private void DoLogin(final String user, final String password, final String verifation) {        new Thread(new Runnable() {            @Override            public void run() {                DefaultHttpClient defaultclient = new DefaultHttpClient();                HttpPost httpPost = new HttpPost(LOGINURL);                httpPost.setHeader("Cookie", Cookie);                HttpResponse httpResponse;                //设置post参数                List<NameValuePair> params = new ArrayList<NameValuePair>();                               String inputText_user = userName.getText().toString();                String inputText_password = passWord.getText().toString();                String inputText_yanzhengma = yanzhengma.getText().toString();                params.add(new BasicNameValuePair("groupId", ""));                params.add(new BasicNameValuePair("j_username", user));                params.add(new BasicNameValuePair("j_password", password));                params.add(new BasicNameValuePair("j_captcha", verifation));                               params.add(new BasicNameValuePair("login", "登录"));                //获得个人主界面的HTML                try {                    httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));                    httpResponse = defaultclient.execute(httpPost);                    Log.i("xyz", String.valueOf(httpResponse.getStatusLine().getStatusCode()));                    if (httpResponse.getStatusLine().getStatusCode() == 200) {                        StringBuffer sb = new StringBuffer();                        HttpEntity entity = httpResponse.getEntity();                        MAINBODYHTML = EntityUtils.toString(entity);                        Log.d("I",MAINBODYHTML);                        //Toast.makeText(bangdingActivity.this, MAINBODYHTML, Toast.LENGTH_SHORT).show();                        IsLoginSuccessful(MAINBODYHTML);                    }                } catch (UnsupportedEncodingException e) {                                        e.printStackTrace();                } catch (ClientProtocolException e) {                                        e.printStackTrace();                } catch (IOException e) {                                        e.printStackTrace();                }            }        }).start();    }

里面的parm参数需要通过httpwatch获取到postdata得到属性。创建post,client response,与前面无异,我们获取数据的Post请求,比如说获取验证码请求是不需要参数的,但是我们登录需要发送给服务器 用户名 密码 验证码,于是我们为Post请求设置参数。

获取到POST DATA就是需要上传的params

3.jsoup解析html

private void IsLoginSuccessful(String loginresult){        Document doc = Jsoup.parse(loginresult);        Message msg = new Message();        //先判断是否登录成功,若成功直接退出        Elements el = doc.select("div[id=error]");        if(el.text().contains("您输入的验证码不正确")){            Log.d("xyz", "验证码错误");            msg.arg1 = 0;            handler.sendMessage(msg);        }        else if(el.text().contains("密码不匹配")) {            Log.d("xyz", "密码不匹配");            msg.arg1 = 2;            handler.sendMessage(msg);        }        else if(el.text().contains("用户名")){            Log.d("1","用户名不存在");            msg.arg1 = 5;            handler.sendMessage(msg);        }        else{            msg.arg1 = 6;            handler.sendMessage(msg);        }    }


1 0
原创粉丝点击