简体   繁体   English

从Android应用程序登录到网站以检索和解析信息

[英]Logging in to website from android app to retrieve and parse information

I've been researching networking in android and have become familiar with the basic concepts. 我一直在研究android中的网络,并且已经熟悉了基本概念。 However, I've recently been working on an app intended for use by my school as a gradebook/assignment display of sorts. 但是,我最近一直致力于将我的学校用作各种类型的成绩簿/作业展示的应用程序。 My school uses the following online gradebook: https://parents.mtsd.k12.nj.us/ , and currently I'm trying to allow students to input their login credentials into my app which will then try to retrieve their respective information and display it in a gridview. 我的学校使用以下在线成绩簿: https ://parents.mtsd.k12.nj.us/,目前我正在尝试让学生将他们的登录凭据输入我的应用程序,然后尝试检索他们各自的信息和在gridview中显示它。 However, I have never attempted to log in to a website from an android app before parsing data (I have gone through several similar questions but I am still a little confused) 但是,在解析数据之前,我从未尝试从Android应用程序登录到网站(我已经经历了几个类似的问题,但我仍然有点困惑)

Any help is appreciated! 任何帮助表示赞赏!

[edit] After logging in I will use import.io to display gradebook data, website does not have api [编辑]登录后我将使用import.io来显示成绩簿数据,网站没有api

If I understand You clearly u need to : 如果我理解你,你需要:

  1. Connect to site using HttpURLConnection with user creditentials (do Post request to login page) 使用HttpURLConnection与用户creditentials连接到站点(对登录页面发送请求)
  2. Obtain cookies from request (my favorite way is to parse - copy appropriate header field) 从请求中获取cookie(我最喜欢的方法是解析 - 复制适当的头字段)
  3. Use obtained cookies with HttpURLConnection.setRequestProperty("Cookie",obtainedCookies) and do another request(to user data page). 使用获得的cookie与HttpURLConnection.setRequestProperty(“Cookie”,obtainCookies)并执行另一个请求(到用户数据页)。
  4. Get input stream 获取输入流
  5. Parse stream (to obtain Document "html" u can use jsoup example: Document doc = JSoup.parse(stream, null, ""); 解析流(获取Document“html”你可以使用jsoup示例:Document doc = JSoup.parse(stream,null,“”);

  6. Show data 显示数据

GUIDELINES: 指南:

cookie how to 饼干怎么样

Most useful example 最有用的例子

Caution: 警告:

-any http requests needs to be done outside main thread (ui thread) - u can use async task for this or intent service - 任何http请求都需要在主线程(ui线程)之外完成 - 你可以为这个或意图服务使用异步任务

EDIT on questions: 编辑问题:

you ask whether to use Jsoup to handle connection? 你问是否使用Jsoup来处理连接?

my answer is NO! 我的回答是不!

Jsoup is a PARSER by using HttpURLConnection u gain full control over HTTP request so for example u can handle some specyfic exceptions or request propertiers which jsoup is not capable! 通过使用HttpURLConnection,Jsoup是一个PARSER,你可以获得对HTTP请求的完全控制 ,例如,你可以处理一些特殊的异常或请求jsoup不具备的属性! from my experience after a while I start to disassemble the libraries and learn from them the basics and use parts of them in my code! 根据我的经验,一段时间后我开始反汇编库并从中学习基础知识,并在我的代码中使用它们的一部分!

您可以使用具有登录名和密码字段的自定义HTML文件创建WebView,然后让HTML文件运行与网站相同的脚本以将其登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM