Android读取网页内容

1、修改AndroidManifest.xml文件

<uses-permission android:name="android.permission.INTERNET" />

2、网页读取类

package com.neohope.android.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class HttpUtils {

    public static String readStringFromUrl(String szUrl,String szCharcode) throws IOException {
        URL url = new URL(szUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");

        String szJson = "";
        int resPonseCode = conn.getResponseCode();
        if (conn.getResponseCode() == 200)
        {
            InputStream is = conn.getInputStream();
            byte[] data = readStream(is);
            szJson = new String(data,szCharcode);
        }

        return szJson;
    }

    private static byte[] readStream(InputStream inputStream) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            bout.write(buffer, 0, len);
        }
        bout.close();
        inputStream.close();

        return bout.toByteArray();
    }
}

跨语言翻译

1、Java翻译为JS

1.1、GWT
通过Eclipse+GWT插件,进行界面设计,界面响应方法与AWT类似。开否人员可以用JavaGUI设计的方式进行复杂界面开否,然后翻译为H5+JS的页面。

1.2、RAP/RWT
通过Eclipse SWT方式来进行界面开发,通过RAP/RWT将页面翻译为H5+JS的页面。可以通过相对可控的代价,将原来CS架构的SWT项目,转换为Web架构。问题也是一样的,就是全部的修改,要从CS架构改起。

2、wrapper
2.1、swig
可以将c/c++项目进行包装,支持Java, JS, GO, C#, Perl, PHP, Python, Ruby, Lua, R, Android等多种语言调用。

Winform多行标题TabControl

今天写示例程序的时候,遇到了需要多行标题TabControl的情况。
整体流程很简单,就是将标题拆分,然后根据行数和最大行宽设置每一Page标题的高度及宽度即可。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace UITabControlTest
{
    public partial class SubForm : Form
    {
        private Dictionary<TabPage, TabPageInfo> allTabMap = new Dictionary<TabPage, TabPageInfo>();

        public SubForm()
        {
            InitializeComponent();
            tabMain.DrawMode = TabDrawMode.OwnerDrawFixed;
        }

        private void SubForm_Load(object sender, EventArgs e)
        {
            tabPage1.Text = "line1\nline2\nline3\nline4\nlin5";
            tabPage2.Text = "line1\nline2\nline3\nline4";
            tabPage3.Text = "line1\nline2\nline3";
            tabPage4.Text = "line1\nline2";

            int lineNum = 0;
            foreach (TabPage tab in this.tabMain.TabPages)
            {
                TabPageInfo pInfo = new TabPageInfo(tab.Text,"\n");
                allTabMap.Add(tab, pInfo);
                lineNum = lineNum>pInfo.LineNum?lineNum:pInfo.LineNum;
            }

            foreach (TabPage tab in this.tabMain.TabPages)
            {
                TabPageInfo pInfo = allTabMap[tab];
                tab.Text = pInfo.MaxLine;
            }

            //some thing is wrong, the font height is 14, the padding height is around 9, why?
            tabMain.Padding = new Point(0, Convert.ToInt32(tabMain.Font.Height * (lineNum - 1) * 0.65));
        }

        private void tabMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            TabControl tabControl = (TabControl)sender;
            TabPage currentTab = tabControl.TabPages[e.Index];
            TabPageInfo info = allTabMap[currentTab];
            string tabText = info.AllLine;
            
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            RectangleF tabRect = (RectangleF)e.Bounds;
            RectangleF textRect = tabRect;
            if (e.Index == tabControl.SelectedIndex)
            {
                tabRect.Inflate(1, 1);
            }

            Graphics g = e.Graphics;
            g.Clip = new Region(tabRect);
            g.Clear(Control.DefaultBackColor);
            g.ResetClip();
            g.DrawString(tabText, e.Font, SystemBrushes.ControlText, textRect, sf);
        }
    }

    class TabPageInfo
    {
        public int LineNum
        {
            get
            {
                return nLineNum;
            }
        }

        public String MaxLine
        {
            get
            {
                //prevent auto wrap
                return szMaxLine+"  ";
            }
        }

        public String getLine(int index)
        {
            if (index < nLineNum)
            { 
                return szLines[index];
            }
            else
            {
                return "";
            }
        }

        public String AllLine
        {
            get
            {
                return szAllLine;
            }
        }

        private int nLineNum = 0;
        private String[] szLines;
        private String szAllLine;
        private String szMaxLine;

        public TabPageInfo(String pageTitle, String splitString)
        {
            szAllLine = pageTitle.Replace(splitString, "\n");
            String [] separators = new String[]{splitString};
            szLines = pageTitle.Split(separators,StringSplitOptions.RemoveEmptyEntries);
            nLineNum = szLines.Length;

            int nMaxLineWidth = 0;
            foreach(String line in szLines)
            {
                if (nMaxLineWidth < line.Length)
                {
                    nMaxLineWidth = line.Length;
                    szMaxLine = line;
                }
            }
        }
    }
}

IIS开启程序集绑定日志

最近,将一个AnyCPU+Win32的程序,调整到了x64架构,但一直各种绑定失败。
IIS7一直提示要开启程序就绑定日志:

警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

好吧,命令行搞定

reg add HKLM\software\microsoft\fusion /v Enablelog /t REG_DWORD /d 1

然后,IIS7仍然坚持不懈的提示绑定失败。提示dll格式不正确。
弄了一个多小时,发现应用程序池的“Enable 32-Bit Applications”选项打开了,晕死。
关闭之,搞定!!!

PS:
按网上的说法,Fuslogvw.exe可以查看绑定失败的日志。但我打开后没有哦,以后再试试看。

CMD常用命令16获取命令行参数

wmic process where caption="java.exe" get caption,commandline /value

"D:\NeoLang\Java\JDK\jdk_x64_1.8.0_77\bin\java.exe"
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-classpath "D:\NeoLang\Java\JavaContainer\apache-tomcat-8.5.16-windows-x64\bin\bootstrap.jar;D:\NeoLang\Java\JavaContainer\apache-tomcat-8.5.16-windows-x64\bin\tomcat-juli.jar"
-Dcatalina.base="D:\NeoLang\Java\JavaContainer\apache-tomcat-8.5.16-windows-x64"
-Dcatalina.home="D:\NeoLang\Java\JavaContainer\apache-tomcat-8.5.16-windows-x64"
-Djava.io.tmpdir="D:\NeoLang\Java\JavaContainer\apache-tomcat-8.5.16-windows-x64\temp"
org.apache.catalina.startup.Bootstrap  start

"C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe"
-Djetty.home=D:/GitHub/Jetty/binary/jetty-distribution-9.4.18.v20190429
-Djetty.base=D:/GitHub/Jetty/binary/jetty-distribution-9.4.18.v20190429
-Djava.io.tmpdir=C:/Users/Hansen/AppData/Local/Temp
-jar D:/GitHub/Jetty/binary/jetty-distribution-9.4.18.v20190429/start.jar jetty.state=D:/GitHub/Jetty/binary/jetty-distribution-9.4.18.v20190429/jetty.state jetty-started.xml

R语言做简单线性规划

首先要安装线性规划扩展包

install.packages("lpSolve")

例1:
某工厂甲、乙两种产品,每件甲产品要耗钢材2kg、煤2kg、产值为120元;每件乙产品要耗钢材3kg,煤1kg,产值为100元。现钢厂有钢材600kg,煤400kg,试确定甲、乙两种产品各生产多少件,才能使该厂的总产值最大?

解:

# 优化方向: 
# maximize
# 目标函数:
# f(X1,X2)=120X1+100X2
# 约束条件:
# 2X1+3X2<=600
# X1+X2<=400

#引用lpSolve
library (lpSolve)

#求解
f.obj <- c (120, 100)
f.con <- matrix ( c (2, 3, 1, 1), nrow = 2, byrow = TRUE )
f.dir <- c ( "<=" , "<=" )
f.rhs <- c (600, 400)
lp.result <- lp ( "max" , f.obj, f.con, f.dir, f.rhs)

#输出结果
lp.result
#Success: the objective function is 36000 

#输出解
lp.result$solution
#[1] 300   0

例2:
某工厂要做100套钢架,每套用长为2.9m,2.1m,1.5m的圆钢各一根。已知原料每根长7.4m,问:应如何下料,可使所用原料最省?(可选方案如下)

可选方案 方案1 方案2 方案3 方案4 方案5
2.9m 1 2 0 1 0
2.1m 0 0 2 2 1
1.5m 3 1 2 0 3
合计 7.4 7.3 7.2 7.1 6.6
剩余料头 0 0.1 0.2 0.3 0.8

解:

# 优化方向: 
# minimize
# 目标函数:
# f(X1,X2,X3,X4,X5)=0X1+0.1X2+0.2X3+0.3X4+0.8X5
# 约束条件:
# X1+2X2 + 0X3 + X4 + 0X5>=100
# 0X1+0X2 + 2X3 + 2X4 + 1X5>=100
# 3X1+X2 + 2X3 + 0X4 + 3X5>=100

#引用lpSolve
library (lpSolve)

#求解
f.obj <- c (0, 0.1, 0.2, 0.3, 0.8)
f.con <- matrix ( c (1, 2, 0, 1, 0, 0, 0, 2, 2, 1, 3, 1, 2, 0, 3), nrow = 3, byrow = TRUE )
f.dir <- c ( ">=", ">=", ">=" )
f.rhs <- c (100, 100, 100)
lp.result <- lp ( "min" , f.obj, f.con, f.dir, f.rhs)

#输出结果
lp.result
#Success: the objective function is 10

#输出解
lp.result$solution
#[1] 100   0  50   0   0

CMD常用命令15获取管理员权限模板

@echo off

:AQUIRE_ADMINISTRATOR_PRIVILEGE
rem 获取管理员权限
rem 首先在%windir%尝试新建文件夹,查看是否有管理员权限
rem 如果新建文件夹成功则删除文件夹,继续操作
rem 如果失败文件夹则新建vbs脚本,通过UAC窗口,获取管理员权限
rem 运行脚本后,删除vbs脚本
md "%windir%\TestAdminPrivilege" &gt; nul
cls
if '%errorlevel%' == '0' (
rmdir "%windir%\TestAdminPrivilege" &amp; goto gotAdmin
) else (goto UACPrompt)
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) &gt; "%temp%\getadmin.vbs"
echo UAC.ShellExecute %0, "", "", "runas", 1  &gt;&gt; "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" (del "%temp%\getadmin.vbs")
CD /D "%~dp0"

:Init
REM 设置窗口标题,窗口颜色,并输出提示信息,通过输入来判断跳转到哪里
cls
title CMD窗口标题
color A
set CHOICE=
echo ----------------------------提示信息标题-------------------------------------
echo.
echo                             提示信息内容
echo                             提示信息内容
echo.
echo ----------------------------------------------------------------------------
echo           您需要以管理员身份运行,或关闭“用户帐户控制(UAC)”
echo ----------------------------------------------------------------------------
:RE_INPUT
echo.
echo  键入 i 安装,键入 r 卸载,键入 q 退出 (键入后请按回车键确认)
set /P CHOICE= ^&gt; 选择:
if /I "%CHOICE%" == "i" goto INSTALL_DETECT
if /I "%CHOICE%" == "r" goto REMOVE
if /I "%CHOICE%" == "q" goto END
goto INPUT_UNKNOWN

:REMOVE
echo  删除提示信息
rem del /F /Q FULL_FILE_PATH
REM reg IMPORT UNINSTALL_DRIVER.REG
goto REMOVE_END

:REMOVE_END
echo  卸载完毕提示信息
goto END

:INSTALL_DETECT
REM 判断32位还是64位系统
if /I "%PROCESSOR_ARCHITECTURE%" == "AMD64" goto INSTALL64
if /I "%PROCESSOR_ARCHITECTURE%" == "x86" goto INSTALL32
goto INSTALL_UNKNOWN

:INSTALL64
echo  将为您安装64位程序
echo  安装提示信息
REM copy /Y FROM_PATH_64 TO_PATH
REM reg IMPORT INSTALL_DRIVER.REG
goto INSTALL_END

:INSTALL32
echo  将为您安装32位程序
echo  安装提示信息
REM copy /Y FROM_PATH_32 TO_PATH
REM reg IMPORT INSTALL_DRIVER.REG
goto INSTALL_END

:INSTALL_END
echo  安装成功提示信息
goto END

:INPUT_UNKNOWN
echo  键入的信息错误,请重试
goto RE_INPUT

:INSTALL_UNKNOWN
echo  未能识别您的系统信息,无法为您安装驱动,您可能需要手动安装
goto END

:END
REM 清理环境变量
echo.
set CHOICE=
pause
# 使用myuid来执行mycmd
# 需要输入密码
runas /noprofile /user:myuid mycmd
# 执行命令时不按enter
# 而是按ctrl+shift+enter
ctrl+shift+enter

AndroidStudio打包so文件

AndroidStudio打包so文件方法如下:
1、将so文件,用下面的文件结构打包为一个zip包,并修改后缀为jar,如armeabi.jar
/lib/armeabi/*.so

2、将armeabi.jar拷贝到项目的libs文件夹下
/your_project/app/libs

3、AndroidStudio会默认将libs下的*.jar一起打包

这样就好啦

Python抓取JSON网页内容

# -*- coding: UTF-8 -*-
'''
Created on 20150206

@author: Hansen
'''

import urllib2
import sys
import io
import json

#Fetch HTML from URL
def fecth_html(index,url,keepHtml,resultFile):
    req = urllib2.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0')
    rsp = urllib2.urlopen(req)
    content = rsp.read()
    #receive_header = rsp.info()
    #print(sys.getfilesystemencoding())
    #content = content.decode('utf-8','replace')
    
    if keepHtml:
        fileinfo = open(str(index)+'.html','w')
        fileinfo.write(content)
        print("save file "+ str(index)+'.html:    ok')
        
    parse_json(content,resultFile)
    
#Parse HTML
def parse_json(content,resultFile):
    jsonData = json.loads(content)
    shops = jsonData['shopBeans']
    print(len(shops))
    for shop in shops:
        szTitle = shop['filterFullName']
        szTitle = szTitle.replace("\r\n", "-").replace(" ","");
        szStar = shop['shopPowerTitle']
        szMeanPrice = str(shop['avgPrice'])
        szMeanPrice = szMeanPrice.replace("\n", "").replace(" ","");
        szAddressA = shop['mainRegionName']
        szAddressB = shop['address']
        szAddress = (szAddressA+"-"+szAddressB).replace("\r\n", "-").replace(" ","");
        szTaste = shop['refinedScore1']
        szEvn = shop['refinedScore2']
        szService = shop['refinedScore3']
        
        fileinfo = io.open(resultFile,'a',encoding='utf_16')
        fileinfo.write(szTitle+","+szStar+","+szMeanPrice+","+szAddress+"," +szTaste+","+szEvn+","+szService+"\n")