Put schools on the table-Android desktop parts that update on-campus data in real-time

Source: Internet
Author: User

Project address: Https://github.com/hwding/AeolosXDUWidget

Support (╭ ̄3 ̄) ╭?

Although this small white school is a xx university, but the network infrastructure is ... The individual pages used for querying are not a system at all ... That is, if you want to be very convenient to get your results of the experiment, the number of sports clock in the flow of the school card and the balance and the use of water and electricity (library borrowing information, etc.), you have to log on many different Web pages using multiple sets of different systems to enter different accounts and passwords ... (Do not want to spit groove some systems actually also outsourced to another xxx uestc ...)

So can you use a convenient way to present and refresh important information in a timely manner?

According to this demand to make a small part of their own mobile phone on the desktop, it is really convenient! It's really convenient! It's really convenient!

Let's put two previews in the picture below.

  

That way, the widget's function is at a glance.

Get the latest data automatically as long as you bind the relevant account

The principle is simple, just log in to get the cookie and then parse the relevant page, and finally render the data

Because of the use of a small wheel that was previously made, it is an interface for querying the data of each module, so the code volume of this part is greatly reduced

Interface Project Address: Https://github.com/hwding/libXDUQuery

Here are some fragmentary points of knowledge

About Verification Code:

In the production of a major difficulty is to query a cartoon water system must lose a verification code

The server will plug in a cookie when you first open the login page, key is Jsessionid, and the credentials for the current session

With this cookie to access a page to return the verification code, you will get a verification code picture, the server side of the current session of the verification code is also refreshed

Submit the user name password with this verification code, as long as the server-side verification code is consistent with the verification code you submitted can return a valid cookie

And then use this cookie to access the query page, OK.

Of course there is the problem of verifying code picture cache, need <uses-permission android:name= "Android.permission.WRITE_ External_storage "/> this permission

Caching methods:

1  Public voidGetcaptcha (File file)throwsIOException {2URL url =NewURL (HOST +captcha_suffix);3URLConnection URLConnection =url.openconnection ();4Urlconnection.setrequestproperty ("Cookie", "jsessionid=" +jsessionid);5 Urlconnection.connect ();6InputStream InputStream =Urlconnection.getinputstream ();7         byte[] bytes =New byte[1024];8FileOutputStream FileOutputStream =Newfileoutputstream (file);9         intLENGTH;Ten          while(LENGTH = inputstream.read (bytes))! =-1){ OneFileoutputstream.write (Bytes, 0, LENGTH); A         } - inputstream.close (); - fileoutputstream.close (); the}

To set the thread used by the page to download the Verification code:

1  PackageXdu.hwding.aeolosxdu;2 3 ImportAndroid.os.Handler;4 ImportAndroid.os.Message;5 ImportJava.io.File;6 Importjava.io.IOException;7 ImportFoopackage.ecard;8 9  Public classCaptchaloaderthreadextendsthread{Ten file file; One ecard ecard; A Handler Handler; -  -Captchaloaderthread (file file, Handler Handler)throwsIOException { the          This. File =file; -          This. Handler =handler; -     } -  +      Public voidrun () { -         Try { +Ecard =Newecard (); A Ecard.getcaptcha (file); atMessage message =NewMessage (); -Message.obj =ecard; - handler.sendmessage (message); -}Catch(IOException e) { - e.printstacktrace (); -         } in     } -}

After the cache is successful, the main thread is notified by bitmap mode:

1Handler Generatehandler (FinalImageView ImageView,Finalfile file) {2         return NewHandler () {3 @Override4              Public voidhandlemessage (Message msg) {5Bitmap Bitmap =bitmapfactory.decodefile (string.valueof (file));6 Imageview.setimagebitmap (bitmap);7Findviewbyid (R.id.add_button). setenabled (true);8 Findviewbyid (R.id.add_button). Setonclicklistener (Generateonclicklistener ((ecard) msg.obj));9             }Ten         }; One}

About HTML parsing:

Analysis results page Extraction of effective information is the most important part, the program is ultimately a crawler

Jsoup is undoubtedly the first choice, see: Parsing HTML data in Java (using a third-party library Jsoup)

I don't repeat it here.

Update cycle for desktop widgets (widgets):

When you create a desktop part Androidstudio automatically generates an XML configuration file that defines some of the properties of the part

1<?xml version= "1.0" encoding= "Utf-8"?>2<appwidget-provider3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Android:configure= "Xdu.hwding.aeolosxdu.NewAppWidgetConfigureActivity"5android:initialkeyguardlayout= "@layout/new_app_widget"6android:initiallayout= "@layout/new_app_widget"7android:minheight= "215DP"8Android:minwidth= "250DP"9Android:previewimage= "@drawable/preview"TenAndroid:resizemode= "Vertical" OneAndroid:updateperiodmillis= "1800000" Aandroid:widgetcategory= "Home_screen" > -</appwidget-provider>

minheight and minWidth attributes define the minimum size of the part separately

ResizeMode defines the direction that can be scaled

Watch out for the pits here!!!

Not read the document of this small white naïve think here can let updateperiodmillis as low as 60000 milliseconds so that can be refreshed every minute ...

However the fact is that the minimum update cycle also has half an hour (1800000ms) ... If you want to be shorter, you can only run one more thread and then broadcast notifications.

For this long time I know the truth tears fall down ...

Put schools on the table-Android desktop parts that update on-campus data in real-time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.