2013年10月29日 星期二

Android - 出現 Unable to execute dex: Multiple dex files define ...

Q:execute時出現Unable to execute dex: Multiple dex files define ...
A:清除不必要或重複的Lib,之後重新refresh->clean

2013年10月23日 星期三

Android - 使用JSON的時候出現org.json.JSONException: Value  of type java.lang.String cannot be converted to..


參考:解决org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

問題:使用android的時候發現一個怪問題,當我使用new JSONObject(jsonstr)時會報exception:

org.json.JSONException: Value  of type java.lang.String cannot be converted to...

而且這個情況只有在2.x(沒實際每個版本測試過,估計是3.0以下都會)出錯,在4.1則正常

解答:因為在我使用的情況是,json字串為http回傳的內容,只要將這個輸出的html編碼改為UTF8 without BOM編碼格式即可.

(但是一般win的筆記本似乎無法更改BOM編碼,因此我是另外下載notepad++更改的,請見上述參考連結)

2013年10月12日 星期六

mac - 更改PATH設定


  1. open terminal
  2. sudo nano /etc/paths
  3. 輸入密碼 後enter
  4. 在最後一行上加入欲加入的新PATH路徑
  5. control+x 並選擇Y 離開
  6. 開啓另一個新的terminal,使用 echo $PATH查看是否已加入新的位置

oracle - how to run sqlldr from remote server

需求:需要執行在遠端伺服器(非本機)上的oracle 使用sqlldr匯入資料

方式:

sqlldr [USER]/[PWD]@//[IP:PORT]/[SID] control=[control file]

[]中的參數都是需要替換為自行使用的

需注意的是執行的本機也須安裝sqlldr tools否則會顯示command not found的錯誤

2013年10月6日 星期日

opencsv - 使用CsvToBean.parse()總是無法成功


參考致上述網址code如下:
 
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Country.class);
String[] columns = new String[] {"countryName", "capital"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);
 
CsvToBean csv = new CsvToBean();
 
String csvFilename = "C:\\sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
 
List list = csv.parse(strat, csvReader);
for (Object object : list) {
    Country country = (Country) object;
    System.out.println(country.getCapital());
}
 

package net.viralpatel.java;
 
public class Country {
    private String countryName;
    private String capital;
 
    public String getCountryName() {
        return countryName;
    }
 
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
 
    public String getCapital() {
        return capital;
    }
 
    public void setCapital(String capital) {
        this.capital = capital;
    }
}

以上Country class為inner class
但執行以上code時csv.parse() (LINE11)都會報錯:
java.lang.RuntimeException: Error parsing CSV!

仔細追了一下opencsv source發現,會在MappingStrategy.createBean()時發生
InstantiationException的錯誤,試驗一下發現原來inner class使用reflection的newInstance()都會出現這問題

因為opencsv中的parse會直接使用newInstance() create new instance,因此使用的TYPE不可為INNER CLASS
至於有沒有可以使用inner class的方式要再查查看

2013年10月5日 星期六

javamail - 使用SMTP(非SSL)寄出信件時一直收到javax.net.ssl.SSLHandshakeException...


參考:Java Mail: SSLHandshakeException when sending email on port 25 without SSL


需求:
  最近一個小工具需使用到JAVAMAIL,奇怪的是我明明使用SMTP(非SSL)寄信,卻一直報SSL的錯誤:

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
    at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)


查了半天才發現,只要關閉下列的Properties設定參數即可:
props.put("mail.smtp.starttls.enable", "true");
(把上一行取消就可)