2015年10月22日木曜日

[Swift][GCM]iOSでGoogle Cloud Messageを使う

iOSでGoogle Cloud Message 3.0を使ったので、はまりどころのメモです。
基本的なことは本家サイトを参照ください。
https://developers.google.com/cloud-messaging/ios/client?ver=swift

APNs用にデータを追加する

データ構造はGCM 2.0に以下のAPNsでも使用できるパラメータを追加すればデバイスに届きます。
1
2
3
4
5
"content_available" : "true"
"notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark"
  }

もし、デバイスに届かない場合はcontent_availableの値を確認しましょう。"true"というStringではなく、true / falseのBoolが正しいです。

Push受信しても鳴動しない

notificationにsoundパラメータを追加しましょう。
https://developers.google.com/cloud-messaging/http-server-ref
https://developer.apple.com/jp/documentation/RemoteNotificationsPG.pdf

アプリ起動していない状態でバックグラウンド受信しない

priorityの追加
priorityパラメータを追加して、10を指定しましょう。
https://developers.google.com/cloud-messaging/http-server-ref
https://developer.apple.com/jp/documentation/RemoteNotificationsPG.pdf
iOSアプリ側の設定
Background fetch modeをONにしましょう。
AppDelegateにて、MinimumBackgroundFetchIntervalをセットします。DefaultはNeverになっているため、Background fetchしません。
1
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

2015年10月14日水曜日

[Swift][GoogleAnalytics]CustomDimensionを追加してイベント送信する

Google Analyticsのイベント送信時にCustomDimensionを追加する方法です。

1
2
3
4
5
6
7
8
9
10
let tracker = GAI.sharedInstance().defaultTracker
let params = GAIDictionaryBuilder.createEventWithCategory(
    category,
    action: action,
    label: label,
    value: value)
    .set(_corpId, forKey: GAIFields.customDimensionForIndex(1))
    .build() as [NSObject : AnyObject]
 
tracker.send(params)


set(_corpId, forKey: GAIFields.customDimensionForIndex(1))でCustomDimensionの要素を追加しています。

2015年10月7日水曜日

[Nexus5][Android 6.0]OTAを手動でUpdateする

Android 6.0のOTA配信が始まりました。
XDAでOTA URLが投稿されています。

http://forum.xda-developers.com/google-nexus-5/general/ref-nexus-5-stock-ota-urls-t2475327

と、いうことでadbコマンドを使用して手動でUpdateしました。


1
2
3
curl -L https://android.googleapis.com/packages/ota/google_hammerhead/8f8cc12f7a9d7561be21f95914f289bda86e402b.signed-hammerhead-MRA58K-from-LMY48M.zip > 8f8cc12f7a9d7561be21f95914f289bda86e402b.signed-hammerhead-MRA58K-from-LMY48M.zip
adb reboot bootloader
adb sideload 8f8cc12f7a9d7561be21f95914f289bda86e402b.signed-hammerhead-MRA58K-from-LMY48M.zip


2015年9月16日水曜日

Android Mリリース目前のAOSP mirrorの容量



久々にAOSP mirrorを作ってみました。
(備考:ローカルにAOSPのミラーを作成)


容量80GB超えたよ

上記の画像を見ていただければ、お分かり頂けると思いますが、容量が81.25GBと良い勢いで増えています。
いまSSD 128GB上に作成していますが、Android Mがリリースされると100GBまで行くんじゃないかと心配です。



とはいえ、頻繁にAOSPのソースを取得する現場ですと、mirrorがある方が効率的ですので導入を推奨いたします。

2015年9月15日火曜日

AOSP mirrorのrepo syncエラー: GPGが更新されない

久々にAOSP mirrorのrepo syncを実行したところ、エラーとなりました。
どうも、repoの更新ができていないよう。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
info: A new version of repo is available
 
 
object 5ea32d135963da5542b78895f95332c6a17bbe11
type commit
tag v1.12.31
tagger Dan Willemsen <dwillemsen@google.com> 1441912006 -0700
 
repo v1.12.31
 
error: cannot run gpg: No such file or directory
error: could not run gpg.
error: could not verify the tag 'v1.12.31'
 
 
warning: Skipped upgrade to unverified version
</dwillemsen@google.com>

ググってみたところ、次の方法で解決できるとのこと。
https://groups.google.com/forum/#!searchin/android-building/repo/android-building/ICajKIQlwC8/hxKulJLZ278J

mv ~/.repoconfig/gnupg ~/.repoconfig/gnupg_OLD

repoのconfig内のgnupgが更新されていないようなので、新規で取ってこいってことかな???
mv実行後、repo syncは無事に完遂しました。

2015年8月21日金曜日

iOS8.xでUITableViewAutomaticDimensionを使うと、セル位置が正常にならない

iOS8.xでTableViewのセル高さの自動調整が可能です。

1
self.tableView.rowHeight = UITableViewAutomaticDimension



ただし、TableViewからセル選択により画面遷移後、戻ってきたときなど再表示をするとセル位置が調整されていないことがあります。 対策として、一度表示したセルの高さを保持しておいて、estimatedHeightForRowAtIndexPathで値を返します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    var cellHeight:Dictionary<string ,="" float=""> = ["":0]
     
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
         
        cell.contentView.updateConstraints()
        self.cellHeight[String(indexPath.item )] = Float(cell.frame.size.height)
         
    }
     
    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(self.cellHeight[String(indexPath.item )] != nil){
            return CGFloat(self.cellHeight[String(indexPath.item )]!)
        }
        return self.tableView.estimatedRowHeight
         
    }
</string>

2015年6月13日土曜日

CoordinatorLayoutで困った時に確認するIssue Tracker

Android Design Support LibraryのCoordinatorLayoutを使った際に、困った既知(known issue)の問題をまとめます。

画面回転でBehaviorが正常に表示されない

AndroidManifest.xmlで、ActivityにconfigChanges属性で画面回転での再生成を追加すると、Behaviorが正常に表示されないことがあります。
ユースケースとしてはLandscapeからPortraitの画面回転で、現象が発生します。


この現象は、すでに本家Issue Trackerに登録されています。
CoordinatorLayout in design support library does not update child size on rotation
layout_behavior view height doesn't restore when keyboard goes down / ActionBar ActionView partially visible

AppBarLayoutがアニメーションしない

タッチを止めると、次のようにAppBarLayoutの部分が中途半端に残ってしまうことがあります。
Google Playアプリのようにアニメーションで全部消す/表示するには、タッチイベントとAppBarLayoutのオフセット位置、およびBehaviorのonNestedFlingを組み合わせて実装しないといけないのかな?
(ベストプラクティスを教えて欲しい)

Toolbar should settle when only partially scrolled

2015年6月6日土曜日

Android Support library v22.2で追加されたTextInputLayoutを使う

Material DesignのText fieldアニメーションが楽々に実装できるレイアウトです。
http://www.google.com/design/spec/components/text-fields.html#text-fields-single-line-text-field


実装は非常に簡単で、EditTextをTextInputLayutで囲うだけです。
1
2
3
4
5
6
7
8
9
10
11
12
13
<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:hintTextAppearance="@style/TextInputLayoutHintAppearance">
    <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="HintText"
            android:ems="10"/>
</android.support.design.widget.TextInputLayout>




各パーツはマテリアルカラーに準じています。

2015年5月29日金曜日

Android Support library v22.2で追加されたFloatingActionButtonを使う

早朝のGoogle I/O 2015のKeynoteと共に最新のAndroid Support libraryがリリースされました。
注目すべきはdesignパッケージです。本格的にマテリアルデザインのコンポーネントが追加されています。

http://developer.android.com/tools/support-library/index.html

Libのインポート

Android StudioでのDesign Support Libraryの追加方法は以下を参照、ですがTYPO(5/29時点)がありますので注意が必要です。
http://developer.android.com/tools/support-library/features.html#design

1
compile 'com.android.support:design:22.2.0'

FloatingActionButton

次のように、レイアウトから使用可能です。

1
2
3
4
5
6
7
<android.support.design.widget.FloatingActionButton
        android:id="@+id/floating_action_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/XXXX"
        android.support.design:backgroundTint="@color/fab_bg"
        android:contentDescription="@string/XXXX"/>
FABのカラーはデフォルト:colorAccentです。もしカラーを変更する場合はandroid.support.design:backgroundTint属性でカラーを指定します。


備考

Design Support Libraryはリリースされたばかりで、まだまだissueが沢山あります。
Lollipop端末でFABを確認したところ、Shadowが正しく表示されませんでした。
FAB doesn't have shadow on Lollipop https://code.google.com/p/android/issues/detail?id=175068 The FloatingActionButton has different margins on Lollipop and pre-Lollipop. https://code.google.com/p/android/issues/detail?id=175330
動作がおかしい場合は、issue trackerを確認することをお勧めします。

2015年4月26日日曜日

IntentsTestRuleを使って、Intentのテストを行う

IntentsTestRuleについて

IntentsTestRuleは、startActivity()でセットしたIntentのデータをテストすることができます。
このクラスはActivityTestRuleを継承したクラスで、各Testの実行前にEspresso-Intentsを初期化し、実行後にEspresso-Intentsをリリースします。
各テスト後にActivityはfinish()されます。

build.gradleにEspresso-Intentsライブラリを追加します。
1
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.1'

サンプルソース

次はユーザー操作によって、Intent.ACTION_CALLのIntentが発生したかどうかテストするサンプルプログラムです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityIntentTest {
 
    @Rule
    public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(
            MainActivity.class);
 
    @Before
    public void stubAllExternalIntents() {
        // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
        // every test run. In this case all external Intents will be blocked.
        intending(not(isInternal()))
                .respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
    }
 
    @Test
    public void callPhone() {
        // call action
        onView(withId(R.id.callButton)).perform(click());
 
        // test
        intended(allOf(
                hasAction(Intent.ACTION_CALL),
                hasData("tel:0123456789"),
                toPackage("com.android.server.telecom")));
 
    }
}
テストクラスは次のように作成します。
  • テストクラスにRunWithアノテーションをつける
  • IntentsTestRuleを生成する、Ruleアノテーションをつける

Intentが発生したかどうかは、次のように判定します。
  • intendedメソッドで意図したIntentが発生しているかチェックする
  • matchメソッドはこちらを参照


  • Activity起動時のIntentセット

    テストを行うActivityの起動用Intentが必要な場合、IntentsTestRule#getActivityIntentメソッドをオーバーライドしてIntentを返す。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @Rule
    public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class) {
     
        /**
        * Activity起動用Intent
        */
        @Override
        protected Intent getActivityIntent() {
            Intent intent = new Intent();
            // Activity起動用のパラメータをセット
            intent.putExtra(KEY_DATA,data);
            return intent;
        }
    };

    参考サイト:
    https://github.com/googlesamples/android-testing

    2015年3月1日日曜日

    EspressoでToast表示のチェックをする

    公式ページにアプリケーションLayer以外のチェック方法が載っています。
    Using inRoot to target non-default windows
    https://code.google.com/p/android-test-kit/wiki/EspressoSamples#Using_inRoot_to_target_non-default_windows

    1
    2
    3
    onView(withText("South China Sea"))
      .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
      .perform(click());

    上記の方法でもToast表示のチェックは行えますが、「non-default windows」であってToastの指定ではありません。
    ということで、自作してしまいましょう。

    ToastはWindowManager.LayoutParams.TYPE_TOASTというパラメータを持っています。
    これは表示するLayerを指定するものです。
    次はToast Layerを指定するMatcherのサンプルプログラムです。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    /**
     * Matcher that is Toast window.
     */
    public static Matcher<Root> isToast() {
        return new TypeSafeMatcher<Root>() {
     
            @Override
            public void describeTo(Description description) {
                description.appendText("is toast");
            }
     
            @Override
            public boolean matchesSafely(Root root) {
                int type = root.getWindowLayoutParams().get().type;
                if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
                    IBinder windowToken = root.getDecorView().getWindowToken();
                    IBinder appToken = root.getDecorView().getApplicationWindowToken();
                    if (windowToken == appToken) {
                        // windowToken == appToken means this window isn't contained by any other windows.
                        // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
                        return true;
                    }
                }
                return false;
            }
        };
    }

    EspressoのViewMatchersモジュールを自作する

    EspressoのViewMatchersクラスには、たくさんの判定モジュールが用意されています。
    ただ、JUnit作成時に「こんなチェックをしたいのに、標準で用意されていない」こともあります。
    このような場合、自作するしかありません。

    次のような作業が必要です。
    1. BoundedMatcherを実装したオブジェクトを戻り値とするモジュールを作成
    2. マッチしているかどうか判定するモジュール、matchesSafelyをオーバーライドして判定ロジックを作成
    3. 何を判定するMatcherなのかを記述するdescribeToをオーバーライドして実装

    Espressoのソースが公開されていますので、参考になります。
    ViewMatchersクラス:https://code.google.com/p/android-test-kit/source/browse/espresso/lib/src/main/java/com/google/android/apps/common/testing/ui/espresso/matcher/ViewMatchers.java

    次はTextViewのColorが一致しているかどうか判定するMatcherのサンプルプログラムです。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    public static Matcher<view> withTextColor(final int resourceId) {
     
            return new BoundedMatcher<view textview="">(TextView.class) {
                private int expectedColor = -1;
     
                private String resourceName;
     
     
                @Override
                protected boolean matchesSafely(TextView textView) {
                    if (expectedColor == -1) {
                        try {
                            expectedColor = textView.getResources().getColor(resourceId);
                            resourceName = textView.getResources().getResourceEntryName(resourceId);
                        } catch (Resources.NotFoundException ignored) {
                            // view could be from a context unaware of the resource id.
                        }
                    }
     
                    if (expectedColor != -1) {
                        return (expectedColor == textView.getCurrentTextColor());
                    } else {
                        return false;
                    }
                }
     
                @Override
                public void describeTo(Description description) {
                    description.appendText("with color from resource id: ");
                    description.appendValue(resourceId);
                    if (null != resourceName) {
                        description.appendText("[");
                        description.appendText(resourceName);
                        description.appendText("]");
                    }
                    if (-1 != expectedColor) {
                        description.appendText(" value: ");
                        description.appendText(String.valueOf(expectedColor));
                    }
                }
            };
        }

    2015年2月26日木曜日

    Swiftでパラメータ付きPOSTリクエストを行う

    SwiftでWebAPIのリクエストを行う際、POSTメソッドを使う必要がありました。
    次は非同期でリクエストを行う、NSURLConnectionクラスのsendAsynchronousRequestメソッドを用いたサンプルソースです。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // URLセット
    let url = NSURL(string: "https://test.url.jp")
    var request : NSMutableURLRequest = NSMutableURLRequest(URL: url!)
     
    // POSTメソッド指定
    request.HTTPMethod = "POST"
     
    // POSTパラメータ
    var bodyData: String = "key1=value1&key2=value2"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
    // ヘッダの指定
    request.setValue("HeaderValue", forHTTPHeaderField: "HeaderName")
     
    //asyncで実行
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: responseHandler)

    2015年2月24日火曜日

    SwiftなCocoa Touch FrameworkでCommonCryptoを使う

    Swiftを使ってCocoa Touch Frameworkを作成しています。
    どうしても、CommonCryptoを使う必要があり手段を探していました。

    以下、参考になったサイトです。
    Importing CommonCrypto in a Swift framework
    CommonHMAC in Swift

    手順は次のようになります。

    1. CommonCryptoというディレクトリを作成
    2. module.mapというファイルを作成
    3. プロジェクト設定のBuild Settings -> Swift Compiler - Search Paths -> Import Pathsに、上記のCommonCryptoディレクトリを指定



    次はサンプルです。 module.mapにSDK内のヘッダファイルのPathを記述します。

    1
    2
    3
    4
    5
    module CommonCrypto [system] {
        header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/CommonCrypto/CommonCrypto.h"
        link "CommonCrypto"
        export *
    }

    このあたり、公式Developerサイトに書いてるのかな?

    2015年2月21日土曜日

    Unit testで非同期処理のWaitをする

    XcodeでiOS開発の勉強をはじめました。
    いきなりですが、UnitTestネタです。テストしないもの、エンジニアにあらずです。

    参考:Writing Test Classes and Methods

    waitForExpectationsWithTimeoutで非同期処理の待ち状態を指定

    標準テストフレームワークのXCTestExpectationとXCTestCase.waitForExpectationsWithTimeoutを組み合わせて実装します。
    手順は次のようになります。

    1. XCTestCase.expectationWithDescriptionをコールして、XCTestExpectationを取得 
    2. XCTestCase.waitForExpectationsWithTimeoutで、waitを指定 
    3. 非同期処理終了のタイミングで、XCTestExpectation.fulfill()。もし、waitForExpectationsWithTimeoutで指定した時間内にコールしなければfailとなる

    サンプルプログラムです。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    func testPerformAsyncRequest(){
        // XCTestExpectationの取得
        let expectation = self.expectationWithDescription("client key")
     
        // 非同期処理のコールバック処理完了後、expectation.fulfill()をコール
        expectation.fulfill()
         
        //waitの時間指定
        self.waitForExpectationsWithTimeout(5, handler: nil)
         
    }

    2015年2月8日日曜日

    コードカバレッジを使って、視覚的/統計的にテスト状況を確認する

    build.gradleの設定

    Code Coverage機能を使用するために、app直下のbuild.gradleに追加します。

    android {
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
    
    +        debug {
    +            testCoverageEnabled = true
    +        }
        }
    }
    

    複数のProjectをImportしている場合、各モジュールのbuild.gradleにtestCoverageEnabledを追加します。

    Code Coverageの実行

    Terminalから次のコマンドを実行します。

    1
    ./gradlew createDebugCoverageReport

    createDebugCoverageReportを実行すると、自動的にandroidTestを行います。
    デバイス(or エミュレータ)が起動していない場合、次のようなエラーが表示されます。

    1
    2
    3
    4
    5
    6
    7
    :app:connectedAndroidTest FAILED         
                   
    FAILURE: Build failed with an exception.
                   
    * What went wrong:
    Execution failed for task ':app:connectedAndroidTest'.
    > com.android.builder.testing.api.DeviceException: java.lang.RuntimeException: No connected devices!

    必ずデバイスかエミュレータを起動して、androidTestを実行できる環境にしておきましょう。

    レポートの観覧

    createDebugCoverageReportが成功すると、各モジュールのOutputにレポートを保存します。
    [PROJECT]/app/build/outputs/reports/coverage/debug/index.html

    次のような表が出力されます。コードの全ライン数に対して、テストが実行されたラインが統計的に確認する事ができます。



    次のように視覚的に、どのコードがテストされていないか確認することも可能です。

    2015年2月1日日曜日

    LocalBroadcastManagerを使って、アプリ内部にのみブロードキャストを行う

    LocalBroadcastManager

    LocalBroadcastManagerはアプリ内の同プロセスに限定して、Broadcastを行う事ができます。
    通常のsendBroadcastでブロードキャストと異なり、次のような利点があります。

    プライベートなデータをアプリ外に残す心配がない。
    他アプリがこのBroadcastを使う事ができない。(セキュリティホールの心配はしなくていい)
    通常のBroadcastに比べ、より効率的アプリ内部へブロードキャストが行える。

    注意点としては「同プロセス」に限定してブロードキャストを行います。言い換えれば、他プロセスにはブロードキャストは行えません。


    用途

    データ管理を行うModleクラスを作成する際、多くの人はServiceで実装すると思います。
    Serviceを使うまでもない軽微なModleなら、ObserverとしてLocalBroadcastManagerを利用してデータの更新通知を行うのもありかと思います。


    サンプルソース

    こちらにJUnit付きのソースをアップしています。
    https://github.com/baroqueworksdev/MyApiDemo_AndroidStudio/commit/cf08208cfd84b916480a08acf6f43fd791ca7da0

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    public class LocalBroadcastController {
        private LocalBroadcastManager mLocalBroadcastManager;
        private IntentFilter mIntentFilter;
        private OnLocalBroadcastController mOnLocalBroadcastController;
     
        public interface OnLocalBroadcastController {
            public void onReceive(Context context, Intent intent);
        }
     
        /**
         * Constructor
         *
         * @param context  Context
         * @param filter   Intent Filter which need to receive an action
         * @param listener onReceive
         */
        public LocalBroadcastController(Context context, IntentFilter filter, OnLocalBroadcastController listener) {
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(context);
            mIntentFilter = filter;
            mOnLocalBroadcastController = listener;
        }
     
        /**
         * Register Receiver
         */
        public void registerReceiver() {
            mLocalBroadcastManager.registerReceiver(mReceiver, mIntentFilter);
        }
     
        /**
         * Unregister Receiver
         */
        public void unregisterReceiver() {
            mLocalBroadcastManager.unregisterReceiver(mReceiver);
        }
     
     
        private BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mOnLocalBroadcastController.onReceive(context, intent);
     
            }
        };
     
    }

    2015年1月25日日曜日

    Android StudioでZXingのライブラリを使う

    Android StudioでZXingのライブラリを使う設定です。
    非常に簡単で、アプリのbuild.gradleに追加するだけです。

    1
    2
    3
    4
    5
    6
    7
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.2'
    +    compile 'com.google.zxing:core:3.1.0'
    +    compile 'com.google.zxing:android-integration:3.1.0'
     
    }

    2015年1月17日土曜日

    Volleyのリクエストにパラメータとヘッダーの値を追加する方法

    Volleyのリクエストにパラメータとヘッダーの値を追加する方法です。
    StringRequestクラスの親クラスであるRequestクラスに、次のようなメソッドがあります。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /**
     * Returns a list of extra HTTP headers to go along with this request. Can
     * throw {@link AuthFailureError} as authentication may be required to
     * provide these values.
     * @throws AuthFailureError In the event of auth failure
     */
    public Map<String, String> getHeaders() throws AuthFailureError {
        return Collections.emptyMap();
    }
     
     /**
     * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
     * {@link AuthFailureError} as authentication may be required to provide these values.
     *
     * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
     *
     * @throws AuthFailureError in the event of auth failure
     */
    protected Map<String, String> getParams() throws AuthFailureError {
        return null;
    }

    StringRequestを生成する際、getHeaders()とgetParams()をオーバーライドすれば、POSTパラメータとヘッダーの値を追加することができます。
    次のソースはオーバーライドによって値を渡すサンプルです。


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    /**
     * Request(StringRequest) with params
     *
     * @param url      request url
     * @param listener listener for Response or Error
     * @param params   value of setting Http Params
     * @param headers  value of setting Http headers
     */
    public void get(String url, final ResponseListener listener, final Map<String, String> params
            , final Map<String, String> headers) {
     
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        listener.onResponse(s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        listener.onErrorResponse(volleyError);
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
     
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }
        };
     
        mRequestQueue.add(request);
    }