Android開發 HTTP GET範例

巨人的肩膀

Android Asynchronous Http Client
A Callback-Based Http Client Library for Android
http://loopj.com/android-async-http/

build.gradle

dependencies {
    //...skip
    compile 'com.loopj.android:android-async-http:1.4.9'
}
`</pre>

Sync!!

### AndroidManifest.xml

<pre>`    &lt;uses-permission android:name="android.permission.INTERNET"/&gt;

    &lt;application
    //...skip
`</pre>

### 畫面配置

[![Screen Shot 2015-12-25 at 23.42.17](http://filitov.twcat.org/blog/wp-content/uploads/2015/12/Screen-Shot-2015-12-25-at-23.42.17-300x207.png)](http://filitov.twcat.org/blog/wp-content/uploads/2015/12/Screen-Shot-2015-12-25-at-23.42.17.png)

### 程式碼

<pre>`    public void btnClick( View view ){
        String url = ((EditText)findViewById(R.id.editText)).getText().toString();
        final TextView tv = (TextView)findViewById(R.id.textView);

        (new AsyncHttpClient()).get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                tv.setText(new String(responseBody, StandardCharsets.UTF_8));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

Android開發 基本「共用偏好設定」範例

參考來源:Android Developers 訓練課程 儲存索引鍵值組

建立基本畫面配置

畫面配置

對應的寫入程式碼

    public void buttonSaveClick(View view){
        String a = ((EditText)findViewById(R.id.editText)).getText().toString();
        int b = Integer.parseInt(((EditText) findViewById(R.id.editText2)).getText().toString());

        SharedPreferences sp = this.getPreferences( this.MODE_PRIVATE );
        SharedPreferences.Editor spe = sp.edit();
        spe.putString( "to.fili.testpref.ooxxoioi.sss", a );
        spe.putInt( "to.fili.testpref.ooxxoioi.ddd", b );
        spe.commit();
    }
`</pre>

### 對應的讀取程式碼

<pre>`    public void buttonLodClick(View view){
        SharedPreferences sp = this.getPreferences( this.MODE_PRIVATE );
        String a = sp.getString("to.fili.testpref.ooxxoioi.sss", "nothing");
        int b = sp.getInt("to.fili.testpref.ooxxoioi.ddd", 0);

        ((TextView)findViewById(R.id.textView)).setText( a );
        ((TextView)findViewById(R.id.textView2)).setText( String.valueOf(b) );
    }

使用 Intent 啟動 android 手機預設的相機

使用 Intent 啟動 android 手機預設的相機

    &lt;uses -feature android:name="android.hardware.camera" android:required="true"&gt;&lt;/uses&gt;
`</pre>

<pre>`    static final int SNAPSHOT = 1;

    public void testClick( View view ){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(cameraIntent, SNAPSHOT);
        }
    }
`</pre>

接收 MediaStore.ACTION_IMAGE_CAPTURE Intent 回傳的結果

<pre>`    &lt;uses -permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses&gt;
`</pre>

<pre>`    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK &amp;&amp; requestCode == SNAPSHOT) {
            Bundle extras = data.getExtras();
            Bitmap cameraBMP = (Bitmap) extras.get("data");
            ImageView iv;
            iv = (ImageView)findViewById( R.id.myimg );
            iv.setImageBitmap(cameraBMP);
        }
    }

Passenger + apache 用homebrew安裝錯誤訊息 Support binary PassengerAgent not found

brew 裝的 Passenger 不曉得哪次升級後掛了,檢查apache的錯誤訊息發現:

*** Passenger could not be initialized because of this error: Unable to start Phusion Passenger: 
Support binary PassengerAgent not found 
(tried: /usr/local/opt/passenger/libexec/lib/phusion_passenger/locations.ini/buildout/support-binaries/PassengerAgent 
and /var/root/.passenger/support-binaries/5.0.20/PassengerAgent). 
This probably means that your Passenger installation is broken or incomplete, 
or that your 'PassengerRoot' setting contains the wrong value. 
Please reinstall Passenger or, 
or see https://www.phusionpassenger.com/library/config/apache/reference/#passengerroot to learn how to adjust the setting.
`</pre>

檢查 `httpd.conf` 發現是依照 `brew info` 裡的指示沒錯,但就是不會動。

<pre>`LoadModule passenger_module /usr/local/opt/Passenger/libexec/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/opt/Passenger/libexec/lib/phusion_passenger/locations.ini
PassengerDefaultRuby /usr/bin/ruby 
`</pre>

依照[指示網址](https://www.phusionpassenger.com/library/config/apache/reference/#passengerroot)

<pre>`In all other cases, obtain the correct value by running the following command:
passenger-config --root
`</pre>

然後`passenger-config --root`取得了

<pre>`/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib/phusion_passenger/locations.ini
`</pre>

用它來改 `httpd.conf` 就好了...

mm...mm...

留一份備忘:
`/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib/phusion_passenger/locations.ini`

<pre>`[locations]
packaging_method=homebrew
bin_dir=/usr/local/Cellar/passenger/5.0.20/libexec/bin
support_binaries_dir=/usr/local/Cellar/passenger/5.0.20/libexec/buildout/support-binaries
lib_dir=/usr/local/Cellar/passenger/5.0.20/libexec/buildout
helper_scripts_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/helper-scripts
resources_dir=/usr/local/Cellar/passenger/5.0.20/libexec/resources
include_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src
doc_dir=/usr/local/Cellar/passenger/5.0.20/libexec/doc
ruby_libdir=/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib
node_libdir=/usr/local/Cellar/passenger/5.0.20/libexec/src/nodejs_supportlib
apache2_module_path=/usr/local/Cellar/passenger/5.0.20/libexec/buildout/apache2/mod_passenger.so
ruby_extension_source_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_native_extension
nginx_module_source_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/nginx_module
build_system_dir=/usr/local/Cellar/passenger/5.0.20/libexec
download_cache_dir=/usr/local/Cellar/passenger/5.0.20/libexec/download_cache

Samsung Galaxy Note 3 hard reset

  1. 關機
  2. 按住 增加音量鍵+HOME鍵+電源鍵 不放,直至開機
  3. 進入回復選單(recovery menu)
  4. 按音量鍵上下選取 wipe data / factory reset ,按電源鍵
  5. 按音量鍵上下選取 reboot system ,按電源鍵