LoginSignup
1
2

More than 5 years have passed since last update.

NiconicoAPIメモ(見よう見まね編)

Last updated at Posted at 2017-03-02

Niconico API ライブラリ

https://github.com/fujiriko59/nico-api-client

にあるものを使ったが、Cookie関連とXMLのパース関連でエラーを吐きまくっていたので一から自分で作ることに。
AndroidでもGladleを用いていたので、プロジェクトはGladleで管理することに。
後日公開予定。

数時間右往左往していたが、

https://teratail.com/questions/31972

を参考にしたらうまくいった
残念ながら筆者はHttp関連の知識が皆無である。

Login
    public static void login_test_from_teratail(){
        try {
            URI uri = new URI("https://secure.nicovideo.jp/secure/login?site=niconico");
            String mail = "ここにメアド";
            String pass = "ここにパスワード";
            HttpPost httpPost = new HttpPost(uri);

            RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
            httpPost.setConfig(requestConfig);

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("mail", mail));
            params.add(new BasicNameValuePair("password", pass));
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpResponse httpResponse = httpClient.execute(httpPost);



            //POST結果
            String response1 = httpResponse.toString();
            String response2 ="";
            for (String str : response1.split(",",0)){
                response2 += str+"\n";
            }

            System.out.println(response2);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

HttpURLConnectionにシフト

コメントでApacheのHttpClientは非推奨というご指摘を頂いたので、自分でHttpURLConnectionを使ってCustomHttpClientを使うことに。
しかしここでも、クッキー関連の問題が多発。
調べたところ、こんな問題があるようだ。
- 地域ごとの時間設定によりクッキーの有効期限が0に(2017/03/02時点では解決)
- クッキーデータがスタックされて優先順位が逆に(未解決)

これにより、SessionIDが正しく取得できなくなる。
そこで以下のサイトのClassを使わせて頂くことに。

http://tomott.cocolog-nifty.com/blog/files/CorrectedCookieManager.java

引用: http://tomott.cocolog-nifty.com/blog/2010/09/javecookie5-5d9.html

前回の編集からだいぶ間が空いてしまった。
覚えていることを記述。

動画データのダウンロードについて

クッキーの準備をして直接動画のURLをたたくと403が返されてしまう。
なのでその前に幾つかのURlにアクセスする必要がある。

64K問題

合計のメソッドの数が64Kを超えるとビルドや実行に失敗する問題。

https://developer.android.com/studio/build/multidex.html

HttpのCookie等の概念の実態

いい記事があったので参考にします

http://qiita.com/mogulla3/items/189c99c87a0fc827520e

VideoViewでsetVideoUriでのクッキーの指定

http://www.binzume.net/diary/2010-10-04:A1

に書いてあるように、Uriは指定できてもクッキーが指定できないから困ったと思いつつ、Android Developer サイトをみてたら、

setVideoURI
void setVideoURI (Uri uri, Map<String, String> headers)

がAPI Level 21で追加されていた。これで解決できそう。

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2