Advertisement
Guest User

Untitled

a guest
Jun 18th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1.  
  2.     public static byte[] compress(String string) throws IOException {
  3.         ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
  4.         GZIPOutputStream gos = new GZIPOutputStream(os);
  5.         gos.write(string.getBytes());
  6.         gos.close();
  7.         byte[] compressed = os.toByteArray();
  8.         os.close();
  9.         return compressed;
  10.     }
  11.  
  12.     public void postData(String html) {
  13.         URL url = null;
  14.         BufferedReader reader = null;
  15.         StringBuilder stringBuilder;
  16.  
  17.         try {
  18.             url = new URL("http://192.168.0.15/android.php");
  19.         //    html = compress(html);
  20.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  21.         conn.setReadTimeout(10000);
  22.         conn.setConnectTimeout(15000);
  23.         conn.setRequestMethod("POST");
  24.         conn.setDoInput(true);
  25.         conn.setDoOutput(true);
  26.  
  27.         Uri.Builder builder = new Uri.Builder()
  28.                 .appendQueryParameter("par", html);
  29.         String query = builder.build().getEncodedQuery();
  30.  
  31.         OutputStream os = conn.getOutputStream();
  32.         BufferedWriter writer = new BufferedWriter(
  33.                 new OutputStreamWriter(os, "UTF-8"));
  34.  
  35.         writer.write(query);
  36.         writer.flush();
  37.         writer.close();
  38.         os.close();
  39.  
  40.  
  41.             conn.connect();
  42.             reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  43.             stringBuilder = new StringBuilder();
  44.  
  45.             String line = null;
  46.             while ((line = reader.readLine()) != null)
  47.             {
  48.                 stringBuilder.append(line + "\n");
  49.             }
  50.             String output = stringBuilder.toString();
  51.             Log.d("httpcliente", "BUSCANDO => [" + output + "]");
  52.     } catch (IOException e) {
  53.         e.printStackTrace();
  54.     }
  55.  
  56. //===========
  57.  
  58.     private class ParseURL extends AsyncTask<String, Void, String> {
  59.  
  60.         @Override
  61.         protected String doInBackground(String... strings) {
  62.             StringBuffer buffer = new StringBuffer();
  63.             try {
  64.                 Log.d("JSwa", "Connecting to ["+strings[0]+"]");
  65.                 Document doc  = Jsoup.connect(strings[0]).get();
  66.                 Log.d("JSwa", "Connected to ["+strings[0]+"]");
  67.                 // Get document (HTML page) title
  68.                 String title = doc.title();
  69.                 Log.d("JSwA", "Title ["+title+"]");
  70.                 buffer.append("Title: " + title + "\r\n");
  71.  
  72.                 // Get meta info
  73.                 Elements metaElems = doc.select("meta");
  74.                 buffer.append("META DATA\r\n");
  75.                 for (Element metaElem : metaElems) {
  76.                     String name = metaElem.attr("name");
  77.                     String content = metaElem.attr("content");
  78.                     buffer.append("name ["+name+"] - content ["+content+"] \r\n");
  79.                 }
  80.  
  81.                 Elements topicList = doc.select("h2.topic");
  82.                 buffer.append("Topic list\r\n");
  83.                 for (Element topic : topicList) {
  84.                     String data = topic.text();
  85.  
  86.                     buffer.append("Data [" + data + "] \r\n");
  87.                 }
  88.  
  89.  
  90.  
  91.                 postData(doc.html());
  92.  
  93.                 Elements scriptElements = doc.getElementsByTag("script");
  94.  
  95.  
  96.                 //    Elements scriptElements = doc.getElementsByTag("script");
  97.                 buffer.append("Variavel resultado1\r\n");
  98.                 for (Element element :scriptElements ){
  99.                     for (DataNode node : element.dataNodes()) {
  100.                         //System.out.println(node.getWholeData());
  101.                         String scriptdata = node.getWholeData();
  102.                         String scriptdata2 = node.toString();
  103.  
  104.  
  105.                         //buffer.append("StriptData [" + scriptdata + "] \r\n");
  106.                         //String resultado1
  107.                         String procurarPor = "codigoinicial";
  108.                         if(scriptdata2.toLowerCase().contains(procurarPor.toLowerCase())){
  109.                         //    buffer.append("StriptData=1 [" + scriptdata2 + "] \r\n");
  110.                         //    System.out.println("StriptData=1 [" + scriptdata2 + "] \r\n");
  111.  
  112.  
  113.  
  114.                             String text    =
  115.                                     "This is the text to be searched " +
  116.                                             "for occurrences of the http:// pattern.";
  117.  
  118.  
  119.                             text = scriptdata2;
  120.                             String patternString = ".*http";
  121.  
  122.                             Pattern pattern = Pattern.compile(patternString);
  123.  
  124.                             Matcher m = pattern.matcher(text);
  125.  
  126.                         }
  127.                     }
  128.                     System.out.println("-------------------");
  129.                 }
  130.             }
  131.             catch(Throwable t) {
  132.                 t.printStackTrace();
  133.             }
  134.  
  135.             return buffer.toString();
  136.         }
  137.  
  138.         @Override
  139.         protected void onPreExecute() {
  140.             super.onPreExecute();
  141.         }
  142.  
  143.         @Override
  144.         protected void onPostExecute(String s) {
  145.             super.onPostExecute(s);
  146.             respText.setText(s);
  147.         }
  148.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement