顯示具有 JAVA 標籤的文章。 顯示所有文章
顯示具有 JAVA 標籤的文章。 顯示所有文章

2012年7月7日 星期六

幫eclipse加上自動提示(Intellisense)程式碼功能

做法如下:
Windows -> Preferences -> Java -> Editor -> Content Assist
在Auto activation triggers for Java 中輸入 .abcdefghijklmnopqrstuvwxyz(,
(預設只有 . 才會檢查提示)

2012年7月3日 星期二

Android get date

Android中取得時間日期的方式跟java是用一樣的方式
 private void setDateTextView() 
 {
        TitleView = (TextView)findViewById(R.id.titleText); 
        
        Calendar rightNow = Calendar.getInstance();
        String[] months = {"1月", "2月", "3月", "4月",
                "5月", "6月", "7月", "8月",
                "9月", "10月", "11月", "12月"};
        
        TitleView.setText(months[rightNow.get(Calendar.MONTH)] + rightNow.get(Calendar.DATE) + "日" + "\n"
        + rightNow.get(Calendar.HOUR) + ":" + rightNow.get(Calendar.MINUTE));   
  
 }

2012年7月1日 星期日

JAVA use SNMP

前製作業
SNMP請到 http://gicl.cs.drexel.edu/people/sevy/snmp/
下載支援SNMP的API
我使用的是Version 1.4.2
以下使用JAVA IDE Eclipse來實驗
將SNMP的source匯入專案
1. 解壓縮剛下載的source_1.4.2.rar
2. 將surce資料夾下的snmp資料夾拖拉到專案例如SNMPTest上
3. Eclipse會自己匯入專案 (注意 : 專案內請不要有同名資料夾例如SNMP資料夾 Eclipse會自己產生)

程式範例:
import snmp.*;
import java.net.*; 

public class SnmpTest {
    public static void main(String[] args) {
         try
            {
                InetAddress hostAddress = InetAddress.getByName("遠端支援SNMP的IP位址");
                String community = "public";
                int version = 0;    // SNMPv1
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
                
                String itemID = "1.3.6.1.2.1.1.1.0";
                System.out.println("Retrieving value corresponding to OID " + itemID);
                
                SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
                SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
                SNMPObject snmpValue = pair.getSNMPObjectAt(1);
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
 
                
                itemID = "1.3.6.1.2.1.1.3.0";
                System.out.println("Retrieving value corresponding to OID " + itemID);
                
                newVars = comInterface.getMIBEntry(itemID);
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
                snmpValue = pair.getSNMPObjectAt(1);
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());

            }
            catch(Exception e)
            {
                System.out.println("Exception during SNMP operation:  " + e + "\n");
            } 

    }

}

2012年6月29日 星期五

JAVA 解析JSON範例

/////////////範例
//  {  
//      "phone" : ["12345678", "87654321"], // 數組  
//      "name" : "yuanzhifei89", // 字符串  
//      "age" : 100, // 數值  
//      "address" : { "country" : "china", "province" : "jiangsu" }, // 對象  
//      "married" : false // 布爾值  
//  }  
  
private static final String JSON =   
"{" +  
    "   \"phone\" : [\"12345678\", \"87654321\"]," +  
    "   \"name\" : \"yuanzhifei89\"," +  
    "   \"age\" : 100," +  
    "   \"address\" : { \"country\" : \"china\", \"province\" : \"jiangsu\" }," +  
    "   \"married\" : false," +  
"}";  
  
try {  
    JSONTokener jsonParser = new JSONTokener(JSON);  
    // 此時還未讀取任何json文本,直接讀取就是一個JSONObject對象。  
    // 如果此時的讀取位置在"name" : 了,那麼nextValue就是"yuanzhifei89"(String)  
    JSONObject person = (JSONObject) jsonParser.nextValue();  
    // 接下來的就是JSON對象的操作了  
    person.getJSONArray("phone");  
    person.getString("name");  
    person.getInt("age");  
    person.getJSONObject("address");  
    person.getBoolean("married");  
} catch (JSONException ex) {  
    // 異常處理代碼  
}