EXAMPLE AS-B:
(explanation of origin in terms of editing)
- connect to website (login page) using user data [using HttpUrlConnection]
- delete cookies
- connect to user data page
- analyze data [using JSoup]
- Present
(using a pair of value / key with an http url connection):
1-2. assuming we have a URL (login page http: //blabla/login.php "), and we want to log in so that we do:
// create & open connection HttpUrlConnection connection = (HttpURLConnection) new URL(adress).openConnection(); // set do output ... connection.setDoOutput(true); String CHARSET = "UTF-8"; // Construct the POST value/key pair data. String data = "login=" + URLEncoder.encode(login, CHARSET) + "&password=" + URLEncoder.encode(password, CHARSET) + "&remember_me=on"; byte[] dataBytes = data.getBytes(CHARSET); // create output stram to write our creditentials OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); // write value/key data to output stream outputStream.write(dataBytes); outputStream.flush(); // connect to url connection.connect(); // now we are connected and we can do other stuff get input strem header response code etc.... int responseCode = connection.getResponseCode(); connection.disconnect();
3. Then we got a second page with user data ( http: //blabla/userdata.php ) (* if we weren’t all redirected, ** we can also reuse the connection or make a request as the next step above)
//we are creating & oppening another connection to new adres as at beginning HttpUrlConnection connection = (HttpURLConnection) new URL(adressToUserData).openConnection(); // but we do not construct user value/kay data & don't create output stream // we just add obtained cookies as request property connection.addRequestProperty("Cookie", _cookie); //connecting connection.connect(); //getting input stram InputStrem is = connection.getInputStream(); //parse data for example with jsoup Document doc = JSoup.parse(is,null""); //show parsed result example in grid view
- to analyze the data, you need to know the structure of the web page after logging in (the part with user ratings); to study the page, you can use the simple tool built into the chrome web browser (right-click and select an item).
then u got the “page” as a document from step 3 and u choose what you need
Elements tableWithGrades = doc.select("table>grades");
u can not select one element as a table row (tr), cell (td), span, div by id or class, name, etc. from HTML - what do you like. You just need to learn the "syntax" of JSoup and get the "simple" knowledge of html :)
EDIT:
"I'm not sure what to do with the InputStream or if I am going to log in correctly, and again, I went through and read several online resources to try to understand this concept, but I'm still confused."
to be clear:
- you need to clearly define the goal (s) - what you want to achieve
- then the way .
Do you want (--goals -):
- data from the school server that are available for logging in
so you need ACT and "THINK", for example WEB BROWSER: (- path -):
- make a request with user credentials (login)
- Capture data for registered users
- analyze the data (get what you want from the data)
- show results
u now know how to make 1.2 from my previous answers
Now you need to think about how to 3.4 - there are many ways - many roads in one place: P is your choice, which you take, but still you need to know these roads :)
So, what part of the data collected and how do you want to present it?
EDIT2:
"However, I'm still a little confused about how to actually send the data (e.g. username / password of the user to the site with HttpURLConnection)
- using a login form (activity) that will collect user data
- provision of data from any other source (optional)
before calling u:
// change post string "?login=xxxx&password=zzz" to byte array * byte[] dataBytes = data.getBytes(CHARSET);
related to:
or before calling u, as in the yr jsoup example:
res.execute();
u need to set String login = "..."; , passwod = "...." why ???
, because your code is called sequential (excluding parts of the parallels) & java usage links
"and how to actually save the cookie ..."
- use persistent storage as: general settings, file, database
- or for a session (for each life cycle pid = vm) a single instance (an example of an application class or any other singleton) or some auxiliary class / variable that GC will not consume during the session.
"and does writing to outputStream do the same as my Jsoup example?"
in your status jsoup uses the well-known constructor template , which does the same as the code I wrote in paragraphs 1-4, but it’s something like: "details about how you do this, not related to to me, because it works, " because you can dig a hole with an ax or a shovel - you get the expected result.
"when you submit login information, why would it be an encoded parameter?"
Imagine your password looks like this: $$$ s-_xxx.php? xxaasfs ?? dfsdfśś %% ___ //// ** "* - try a browser request with this Kind URL http://server.com/home.php&action=login&username=xxx&password= $$$ s-_xxx.php? xxaasfs? ? dfsdfśś :)))
URLEncoder.encode(password, CHARSET) /** * This class is used to encode a string using the format required by * {@code application/x-www-form-urlencoded} MIME content type. * * <p>All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') * and characters '.', '-', '*', '_' are converted into their hexadecimal value * prepended by '%'. For example: '#' -> %23. In addition, spaces are * substituted by '+'. */
SOME OTHER TIPS:
- when u open the stream , you should close it when you are done
- to close the stream the "best way" (not always) is to use finally block for try / catch
- before you start checking with a string, if it is not equal to zero, it will save your time
- If you use jsoup to check elements.size ()> 0 or element! = Null before you execute any lines / elements containing actions
ps. for the case youre you should also look at
Using forms-based login in JavaServer Faces web applications
I hope this gives you an overview of one of the possible ways and sorry for the typos, I don't know English, p in general