廣告

2016年3月28日 星期一

Hibernate openSession() vs getCurrentSession()


      SessionFactory.openSession():
      always opens a new session that you have to
      close once you are done with the operations.
     
      SessionFactory.getCurrentSession():
      returns a session bound to a context - you don't need to close this. 
     
      should always use "one session per request" or "one session per transaction"
      In one application, if the DAO layer, using Spring hibernate,
      control the life cycle via Spring session to ,

      First choice  getCurrentSession ().

2016年3月17日 星期四

Usage of @JsonView (annotation of spring)


    public class View {
      interface Summary {}
      interface SummaryWithDetail extends Summary{}
    }
    -----------------------------------
    public class User { 
      @JsonView(View.Summary.class) 
      private Long id; 
      @JsonView(View.SummaryWithDetail.class) 
      private String firstname; 
    }
    -----------------------------------
    @RequestMapping("/user")
    @JsonView(View.Summary.class) 
    //or @JsonView(View.SummaryWithDetail.class) 
    public List getUsers(){
     return userService.listUsers();
    }
    -----------------------------------
    result of @JsonView(View.Summary.class)
    [
     {
        "id": 70,
     }
    ]
    
    result of @JsonView(View.SummaryWithDetail.class)
    [
     {
        "id": 70,
        "firstname": 222
     }
    ]

ref:
http://www.jianshu.com/p/633d83dd303b#

2016年3月10日 星期四

Apache_httpd_to_Tomcat-Mod_Proxy_Setup.txt | 轉址


sudo vi /opt/lampp/etc/httpd.conf

Make sure the following are uncommented:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Add these:
ProxyPass         /eInvoiceLocate http://localhost:8080/eInvoiceLocate
ProxyPassReverse  /eInvoiceLocate http://localhost:8080/eInvoiceLocate

Reload Apache conf
# cd /opt/lampp/bin
# sudo ./apachectl -k graceful


2016年2月25日 星期四

apache xmlhttprequest cannot load origin is not allowed by access-control-allow-origin |ajax 跨網頁存取圖片遇到cors

修改這個檔:
/opt/lampp/etc/httpd.conf

在 <Directory "/opt/lampp/htdocs"> 區段加入:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "x-requested-with"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"

重起
sudo /opt/lampp/bin/apachectl -k graceful

關於cors

Android Receive SMS Tutorial | android 取得sms內容


testReadSms.java
public class testReadSms extends BodyFragment{

    private SmsBroadcastReceiver receiver = new SmsBroadcastReceiver(new SmsBroadcastReceiver.UpdateSmsEvent() {
        @Override
        public void updateSms(String sms) {
            newMsg.setText(sms);
        }
    });

    private ArrayList smsMessagesList = new ArrayList();
    private ListView smsListView;
    private ArrayAdapter arrayAdapter;
    private TextView newMsg;

    @Override
    public void onStart() {
        super.onStart();

        //regist sms receiver
        Log. e("registerReceiver", "registerReceiver");
        receiver.registerSmsReceiver(activity);
    }

    @Override
    protected View fragmentLayout(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.test_body_sms, container, false);
    }

    @Override
    protected void setupComponents(View fragmentView) {
        newMsg = (TextView) fragmentView.findViewById(R.id. new_msg);
        smsListView = (ListView) fragmentView.findViewById(R.id. SMSList);
        arrayAdapter = new ArrayAdapter( activity, android.R.layout.simple_list_item_1 , smsMessagesList);
        smsListView.setAdapter(arrayAdapter);
        smsListView.setOnItemClickListener( this);

        refreshSmsInbox();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //activity.unregisterReceiver(receiver);
    }

    public void refreshSmsInbox() {
        ContentResolver contentResolver = activity.getContentResolver();
        Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null , null, null);
        int indexBody = smsInboxCursor.getColumnIndex( "body");
        int indexAddress = smsInboxCursor.getColumnIndex( "address");
        if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
        arrayAdapter.clear();
        do {
            String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
                    "\n" + smsInboxCursor.getString(indexBody) + "\n";
            arrayAdapter.add(str);
        } while (smsInboxCursor.moveToNext());
    }

    public void updateList(final String smsMessage) {
        arrayAdapter.insert(smsMessage, 0);
        arrayAdapter.notifyDataSetChanged();
    }

    public void updateNewMsg(final String msg) {
        newMsg.setText(msg);
    }

}


SmsBroadcastReceiver.java
public class SmsBroadcastReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";
    public Activity activity;
    public UpdateSmsEvent updateSmsEvent;


    public interface UpdateSmsEvent{
        void updateSms(String sms);
    }

    public SmsBroadcastReceiver(UpdateSmsEvent inte){
        updateSmsEvent = inte;
    }


    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            String smsMessageBody= "";
            for (int i = 0; i < sms. length; ++i) {
                SmsMessage smsMessage = SmsMessage. createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                //smsMessage.getTimestampMillis() is current time
                smsMessageStr += "SMS From: " + address + "\n" ;
                smsMessageStr += smsBody + "\n" ;
                smsMessageBody = smsMessage.getMessageBody().toString();
            }
            Toast.makeText (context, smsMessageBody, Toast.LENGTH_SHORT).show();
            smsMessageBody = smsMessageBody.replaceAll("\\D+","" );
            //this will update the UI with message
            updateSmsEvent.updateSms(smsMessageBody);
            activity.unregisterReceiver(this);
        }
    }

    public void registerSmsReceiver(Activity activity){
        this.activity = activity;
        IntentFilter filter = new IntentFilter();
        filter.setPriority(999 );
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        activity.registerReceiver(this, filter);
    }
}



main ref:

ref:

[android] sms emulator control in Android Studio | android模擬器 模擬簡訊寄送

Tools->Android->Android Device Monitor-> Click on the emulator name in devicess-> Emulator Controls

[android] phone call using intent in android


String phone = location.getPhoneNumber();
//Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
//need click call
Intent intent = new Intent(Intent. ACTION_CALL, Uri.fromParts( "tel", phone, null ));
//call directly
activity.startActivity(intent);


add permission in AndroidManifest.xml