Java Batch Collection Pea pod website Android app icon and package name _java

Source: Internet
Author: User

The theme of Android theme developers, if you want to replace the Third-party application icon, you must know the application of the package name. Actually want to know the application of the package name is very simple, directly in the browser to open Google Play or pea pod, open an application page, look at the URL you will find that the URL last "/" after the character is the application of the package name!

It is estimated that some people want to use the commonly used icons and package names to get down, so in Java wrote a small program, batch grabbed peas pod "all software" according to the total download volume ranked 1 to 20 pages of the application icon and package name.

All the icons are named with the package name, and there is a PackageName.txt file, which contains the name of the package to be used for the search.

Java source

Share this Java applet, note that if the Web page structure of pea pods has changed (it is estimated that little change), this small program needs to be modified, if you can understand, the modification is very simple slightly.

The following code may have been invalidated for reference only!

Copy Code code as follows:

Package im.garth.AppIconDownloader;

Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.URL;
Import java.net.URLConnection;
Import Java.util.HashMap;
Import Java.util.Map.Entry;

Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.HttpStatus;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.util.EntityUtils;
Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;

/**
* Get the Pea pod page on the Android software all software
* Note: Before executing the program, be sure to create icon folder in this engineering directory
* All icons will be downloaded to icon in this folder
* Application name and package name written to the icon under the PackageName.txt file
*
* The jar package used by this program:
* Commons-logging-1.1.3.jar
* Httpclient-4.1.2.jar
* Httpcore-4.3.jar
* Jsoup-1.6.1.jar
*
*
*/
public class Appicondownloader {

/**
* @param args
*/
public static void Main (string[] args) {

String Rooturl = "http://www.wandoujia.com/tag/all software/total?page=";
String Rooturl = "http://www.wandoujia.com/tag/all game/total?page=";
Download 1 to 20 page application icons
for (int i = 1; i < = i++) {
System.out.println ("Download progress": Ready to download the first "+ i +" page ");
String Currenturl = Rooturl + i;
Hashmap<string, string> apps = new hashmap<string, string> ();
Apps = Getappimageurl (Currenturl);
Traverse hashmap Download Icon by individual
For (entry</string><string, string> entry:apps.entrySet ()) {
try{
Download icon, stored in the current project directory under the icon directory (please create icon directory in advance)
Download (Entry.getvalue (), "icon/" + entry.getkey () + ". png");
}catch (Exception e) {
System.out.println ("Download Error": "+ Entry.getkey ());
E.printstacktrace ();
}
}
System.out.println ("Download Progress": "+ i +" page download Complete ");
}

}

/**
* Get all application names and their icon URLs in the URL page
* @param apppackagename
* @return
*/
private static hashmap</string><string, string> getappimageurl (string url) {

Hashmap</string><string, string> apps = new hashmap</string><string, string> ();
String apppackagename = "";
String Appimageurl = "";
String appName = "";

String html = gethtmlbyurl (URL);
Document doc = jsoup.parse (HTML);
Elements Elements = Doc.select ("div.container.clearfix>section.main-col>div.app-blocks>div.app-block> Ul.app-list.clearfix>li.app>a.icon-area ");
Elements nameelements = Doc.select ("Div.container.clearfix>section.main-col>div.app-blocks>div.app-block >ul.app-list.clearfix>li.app>div.operate>a.name>span.txt ");
Elements Imageele;

     int i = 0;
     for (Element ele:elements) {
      //get package name
       apppackagename = ele.attr ("DATA-PN");
      //get icon URL
      imageele = Ele.select ("Img.icon");
      appimageurl = imageele.get (0). attr ("src"). Replace ("68_68", "256_256");
      //Join Apps
      apps.put (AppPackageName, Appimageurl);
      //Get app name
      appname = Nameelements.get (i). Text ( );
      //The app name and package name to the file
      write2file ("" + AppName + "" " + AppPackageName);
      i++;
    }
     System.out.println ("Download Progress": "+ URL +" Under the icon URL has been successful ");
  return apps;
 }

/**
* Download files to local
*
* @param urlstring
* Downloaded file Address
* @param filename
* Local file name
* @throws Exception
* Various anomalies
*/
private static void Download (string urlstring, String filename) throws Exception {
System.out.println ("Download progress": Downloading "+ filename);"
Constructing URLs
URL url = new URL (urlstring);
Open connection
URLConnection con = url.openconnection ();
Input stream
InputStream is = Con.getinputstream ();
1K of data buffering
Byte[] bs = new byte[1024];
The length of the data read
int Len;
File stream for output
OutputStream OS = new FileOutputStream (filename);
Start reading
while (len = Is.read (BS))!=-1) {
Os.write (BS, 0, Len);
}
Complete, close all links
Os.close ();
Is.close ();
System.out.println ("Download Progress": "+ filename + download successful");
}

/**
* Get all the HTML information based on the URL
* @param URL
* @return HTML
*/
private static string Gethtmlbyurl (string url) {
String html = null;
Creating HttpClient Objects
HttpClient httpclient = new Defaulthttpclient ();
Request this URL in get mode
HttpGet httpget = new HttpGet (URL);
try {
Get Responce Object
HttpResponse responce = Httpclient.execute (HttpGet);
Return code
int resstatu = Responce.getstatusline (). Getstatuscode ();
200 normal, the other is wrong.
if (RESSTATU==HTTPSTATUS.SC_OK) {
Get the appropriate entity
httpentity entity = responce.getentity ();
if (entity!=null) {
Get HTML source code
html = entityutils.tostring (entity);
}
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Access" "+url+" "Abnormal!");
E.printstacktrace ();
finally {
Httpclient.getconnectionmanager (). Shutdown ();
}
return HTML;
}

private static void Write2file (String content) {

  file file = new file ("Icon/packagename.txt");
  bufferedwriter writer = null;
  try{
   if (!file.exists ()) {
    file.createnewfile ();
&NBSP;&NBSP;&NBSP}
   //parameter True indicates that the output is appended to the end of the file content without overwriting the original
   writer = new BufferedWriter (New FileWriter (file, true));
   //output
   writer.write (content);
   //the newline
   writer.newline ();
&NBSP;&NBSP} catch (IOException e) {
   system.out.println ("error Output");
   e.printstacktrace ();
  } finally {
   if (writer!= null) {
    try {
      writer.close ();
    } catch (IOException e) {
     e.printstacktrace ();
&NBSP;&NBSP;&NBSP;&NBSP}
   }
  
 }

}

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.