J'ai toujours voulu pratiquer la saisie des données Java multiples.
Un jour, j'ai découvert que le site officiel de Rings Duoduo (http://www.shoujiduoduo.com/main/) a une grande quantité de données.
En observant leur Ajax frontal pour obtenir des données de sonnerie
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getList&listId=;
Il est facile de constater qu'en modifiant la liste et la page, vous pouvez obtenir les données JSON de la sonnerie du serveur et en analysant les données JSON,
Vous pouvez voir qu'ils ont tous des instructions comme {"Hasmore": 1, "Curpage": 1}. En jugeant la valeur de Hasmore, nous décidons de ramper la page suivante.
Cependant, l'adresse de téléchargement dans JSON est retournée via le lien ci-dessus sans sonneries n'est pas disponible en JSON
Vous constaterez bientôt que vous le verrez en cliquant sur le "téléchargement" sur la page.
Grâce à la demande suivante, vous pouvez obtenir l'adresse de téléchargement de la sonnerie
http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getUrl&act=down&rid= {Idringtone id}
Par conséquent, leurs données sont facilement volées. Alors j'ai commencé ...
Le code source a été publié sur GitHub. Si vous êtes intéressé par les chaussures des enfants, veuillez le vérifier
github: https://github.com/yongbo000/duoduoaudiorobot
Sur le code:
Package me.yongbo.duoduoringrobot; import java.io.bufferedeader; import java.io.file; import java.io.filewriter; importer java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.url; importer java.net.net.urlconneur; import java.net.url; import import java.net.net.urtread; import; java.util.iterator; import java.util.regex.matcher; import java.util.regex.pattern; import com.google.gson.gson; import com.google.gson.jsonArray; import com.google.gson.jsonElement; import com.google.gson.jsonparser; / * * @ahor yong 2013/4/16 * * * / classe publique DuoduoringRobotClient implémente Runnable {public static String get_ringinfo_url = "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getList&listid=%1$d&page=%2$dd" "http://www.shoujiduoduo.com/ringweb/ringweb.php?type=getUrl&act=down&rid=%1$d" % 1 $ d, page actuelle:% 2 $ d "; public static String file_dir =" e: / ringData / "; public static string string file_name =" listId =% 1 $ d.txt "; private boolean errorflag = false; private int listid; private int page; private int endPage = -1; private int hasmore = 1; private dbhelper dbhelper; @Param Page Démarrer le numéro de page * @Param EndPage Fin Numéro de page * * / public DuoduoriingRobotClient (int listId, int BeginPage, int EndPage) {this.listId = listId; this.page = beginPage; this.endpage = endpage; this.dbhelper = new dbhelper ();} / ** * Constructeur * @Param list menu id * @Param Numéro Page * Page ** ** ** Construct * / public duoduoriingRobotClient (int listId, int page) {this (listId, page, -1);} / ** * get sonnerie * * / public void getrings () {String url = string.format (get_ringinfo_url, listId, page); string responsablest getNextPage (Responsestr); RingParse (Responsestr.ReplaceALL ("// {/" Hasmore / ": [0-9] *, /" Curpage / ": [0-9] * //},", "") .replaceall (",]", "]"));} / ** * Initiate http request * @Param webrack request Connexion * / ** * Public HTTP demande * @Param webroll httpget (string wearl) {url url; urlConnection conn; stringBuilder sb = new StringBuilder (); string resultstr = ""; try {url = new url (weurl); Conn = url.openconnection (); Conn.Connect (); InputStream is = Conn.getStream (); entréeStreamReader isR = new isr isr isr isr isre isr isr isr isr isr isr = new InputStreamReader (IS); buttereDreader bufReader = new BufferedReader (ISR); String lineText; while ((lineText = bufreader.readline ())! = null) {sb.append (lineText);} Resultstr = sb.toString ();} catch (exception e) {errorflag = true; // écrire une erreur à txtwriteTofile (string.format (error_msg, listy); Objet et enregistrez-le dans txt * @param json JSON String * * / public void ringsParse (String JSON) {ring ring = null; jsonElement element = new JSonParser (). Parse (json); jsonArray array = element.getasjsonArray (); // Traverse the Array Iterator <JSonElement> it = Array.iterator (); gon gon););); (it.hasnext () &&! ErrorFlag) {jsonElement e = it.next (); // convertir jsonElement en javabean object ring = gson.fromjson (e, ring.class); ring.setdownUrl (getringDowrl (ring.getid ())); if (isavailring (ring))) {System.out.println (ring.toString ()); // Vous pouvez choisir d'écrire dans la base de données ou sur text // writeTofile (ring.toString ()); writeTodatabase (ring);}}} / ** * écriture à TXT * @param data String * * / public Void WriteTofile, String Data) {String Path = File_Dir + String. listId); file dir = new file (file_dir); file file = new file (path); filewriter fw = null; if (! dir.exists ()) {dir.mkdirs ();} try {if (! file.exists ()) {file.createenewFile ();} fw = new Filewriter (file, file, file. true); fw.write (data); fw.write ("/ r / n"); fw.flush ();} catch (ioException e) {// todo-généré auto-généré Blocke.printStackTrace ();} enfin {try {if (fw! = null) {fw.close ();}} catch (ioException e) Catch Blocke.printStackTrace ();}}} / ** * Écrivez dans la base de données * @Param Ring une instance de Ring * * / public Void WriteTodatabase (Ring Ring) {dbhelper.execute ("Addring", Ring);} @ OverRidepubl Void Run () {while (hasore == 1 &&! -1) {if (Page> EndPage) {Break; }} System.out.println (string.format (status_msg, listId, page)); getRings (); System.out.println (String.Format ("Les données écrites sur cette page sont terminées"));} System.out.println ("Ending ...");} private int gethasmore (String Resultsstr) {Pattern p = Pattern.Compile ("/" Hasmore / ": ([0-9] *), /" Curpage / ": ([0-9] *)"); Matcher Match = P.Matcher (RUSTURDSTR); if (match.find ()) {return Integer.ParseInt (match.group (1)); } return 0;} private int getNextPage (String resultsTr) {Pattern p = motive.compile ("/" hasmore / ": ([0-9] *), /" curPage / ": ([0-9] *)"); matchateur match = p.matcher (Resultetr); if (match.find ()) {return Integer. Déterminez si l'anneau actuel répond à la condition. Lorsque le nom de l'anneau est supérieur à 50 caractères ou que la durée est décimale, elle ne répond pas aux conditions et sera supprimée. * @Param Ring Current Ring Object instance * * / private boolean isAvailablering (ring ring) {motif p = motive.compile ("^ [1-9] [0-9] * $"); Matcher Match = p.matcher (ring.getDuration ()); if (! Match.Find ()) {return false;} if (ring.getName (). Londres ()> 50 || || ring.getDownUrl (). Length () == 0) {return false;} return true;} / ** * Obtenez l'adresse de téléchargement de la sonnerie * @param rid ringtone id * * / public String getringdownUrl (String rid)