Retrofit 2.0-beta で ログを吐かせる

Retrofit

Okhttp 2.6.0+ では HTTPリクエスト/レスポンスのログ出力が可能なのだが,

New Logging Interceptor. The logging-interceptor subproject offers simple request and response logging. It may be configured to log headers and bodies for debugging. It requires this Maven dependency:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
client.networkInterceptors().add(logging);

okhttp/CHANGELOG.md at master · square/okhttp

残念なことに retrofit 2.0.0-beta2 での同梱は 2.5.0 .

<!-- Dependencies -->
<android.version>4.1.1.4</android.version>
<android.platform>16</android.platform>
<okhttp.version>2.5.0</okhttp.version>
<animal.sniffer.version>1.14</animal.sniffer.version>

retrofit/pom.xml at master · square/retrofit

なので logging-interceptor 2.6.0 で.

    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp</artifactId>
      <version>${project.version}</version>
    </dependency>

okhttp/okhttp-logging-interceptor at master · square/okhttp

compile 'com.squareup.okhttp:logging-interceptor:2.6.0' 
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
logging.setLevel(Level.BASIC);
OkHttpClient client = new OkHttpClient();  
httpClient.interceptors().add(logging);
Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient)
   .build();

出力.

...
11:33:04.547 17090-17108 D/OkHttp: <-- HTTP/1.1 200 OK (469ms, 259-byte body)
11:33:04.550 17090-17296 D/OkHttp: --> GET /v5/item/1072964 HTTP/1.1
11:33:04.563 17090-17263 D/OkHttp: <-- HTTP/1.1 200 OK (250ms, 244-byte body)
11:33:04.566 17090-17108 D/OkHttp: --> GET /v5/item/1073510 HTTP/1.1
11:33:04.606 17090-17265 D/OkHttp: <-- HTTP/1.1 200 OK (252ms, 228-byte body)
11:33:04.609 17090-17263 D/OkHttp: --> GET /v5/item/1072585 HTTP/1.1
11:33:04.794 17090-17296 D/OkHttp: <-- HTTP/1.1 200 OK (244ms, 183-byte body)
11:33:04.796 17090-17265 D/OkHttp: --> GET /v5/item/1073275 HTTP/1.1

stable で対応するのかな.



android-apt で アノテーションを便利に

このプラグインを追加すると、gradle のソースフォルダとして認識される場所に Butter Knife で生成されるコードが格納されます。
そのため、Butter Knife で生成されたクラスも Lint から見えるようになり、Lint が怒らなくなります。やったー。

Y.A.M の 雑記帳: Butter Knife のフィールドやメソッドが Lint に Unused declaration と言われないようにする方法

JakeWharton commented on Sep 26
If you use the android-apt plugin then Android Studio can see the generated code and the method will not show up as unused.

Lint: Unused resource · Issue #356 · JakeWharton/butterknife

Bitbucket で公開されています.

The android-apt plugin assists in working with annotation processors in combination with Android Studio. It has two purposes:
Allow to configure a compile time only annotation processor as a dependency, not including the artifact in the final APK or library
Set up the source paths so that code that is generated from the annotation processor is correctly picked up by Android Studio.

hvisser / android-apt — Bitbucket

「APT」て何?

Annotation Processing Boilerplate Destruction (Droidcon NYC 2014) // Speaker Deck

Butter Knife 以外にも使えるのか.

わーい.


Genymotion で Marshmallow

「Preview」ということですが.

Virtual_device_creation_wizard

Genymotion_for_personal_use_-_PREVIEW_-_Google_Nexus_5X_-_6_0_0_-_API_23_-_1080x1920__1080x1920__420dpi__-_192_168_58_101

とりあえずは動く.

Genymotion

[GUIDE] Genymotion | Installing ARM Translat… - Pg. 13 | Android Development and Hacking | XDA Forums


RecyclerView アイテム内ある要素の クリックイベント

いろんな実装が可能なのですが.


public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.ViewHolder> {

  // ...

  private static ItemsAdapter.OnItemClickListener listener;

  public void setOnItemClickListener(ItemsAdapter.OnItemClickListener listener) {
    ItemsAdapter.listener = listener;
  }

  public interface OnItemClickListener {
    void onItemClick(View view, int position);
  }

  // ...

  public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ViewHolder(final View itemView) {
      super(itemView);

      name = (TextView) itemView.findViewById(R.id.name);
      age = (TextView) itemView.findViewById(R.id.age);
      button = (Button) itemView.findViewById(R.id.button);

      name.setOnClickListener(this);
      age.setOnClickListener(this);
      button.setOnClickListener(this)
    }

    @Override
    public void onClick(View v) {
      if (listener != null) {
        listener.onItemClick(v, getLayoutPosition());
      }
    }

  }
}

UI.


public class MainFragment extends Fragment
    implements ItemsAdapter.OnItemClickListener {

  // ...

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   
    // ...
    mAdapter.setOnItemClickListener(this);
  }

  @Override
  public void onItemClick(View view, int position) {
    switch (view.getId()) {
      case R.id.name:
        // ...
        break;  
        // ...
  }

    // ...
}

ここらなんだろうな.

java - Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview? - Stack Overflow


Android で nmap

外からでなく端末内からみる.

anmap_-_Android_Network_Mapper__network_scanner_original_nmap_for_android__-_Google_Project_Hosting

anmap - Google Project Hosting

端末内にて.

$ tar xvf nmap-5.51-1.tar
tar: invalid tar magic

ん?

$ md5 nmap-5.51-1.tar
ed692f7ea2e217ca71ae1103f5bf3819  nmap-5.51-1.tar
$ ls -al $(which tar)
lrwxr-xr-x root     shell             2015-10-07 13:51 tar -> busybox
$ busybox
BusyBox v1.22.1 bionic (2015-10-06 21:51 -0700) multi-call binary.
…

新しいものに変える.

20151114-080451

BusyBox - Google Play の Android アプリ

$ busybox
BusyBox v1.24.1-Stericson (2015-11-06 19:11:24 EST) multi-call binary.
…

$ tar xvf nmap-5.51-1.tar
nmap
nmap-os-db
nmap-payloads
nmap-protocols
nmap-rpc
nmap-service-probes
nmap-services
$ ./nmap
/system/bin/sh: ./nmap: can't execute: Permission denied
$ su
# chmod +x nmap
Bad mode
# chmod 777 nmap
# ./nmap
sh: ./nmap: can't execute: Permission denied

ん?

# ls -al nmap                                                
-rw-rw---- root     sdcard_r  1907224 2015-11-14 07:45 nmap
# cp nmap* /data/local/tmp
# cd /data/local/tmp
# ls
nmap
nmap-5.51-1.tar
nmap-os-db
nmap-payloads
nmap-protocols
nmap-rpc
nmap-service-probes
nmap-services

# chmod 777 nmap
# ls -al nmap 
-rwxrwxrwx root     root      1907224 2015-11-14 07:48 nmap
# ./nmap 127.0.0.1

Starting Nmap 5.51 ( http://nmap.org ) at 2015-11-13 22:53 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00011s latency).
All 1000 scanned ports on localhost (127.0.0.1) are closed

Nmap done: 1 IP address (1 host up) scanned in 0.64 seconds

ネットワークADB向けをあける.

20151114-075706

# ./nmap 127.0.0.1

Starting Nmap 5.51 ( http://nmap.org ) at 2015-11-13 22:56 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00012s latency).
Not shown: 999 closed ports
PORT     STATE SERVICE
5555/tcp open  freeciv

Nmap done: 1 IP address (1 host up) scanned in 0.57 seconds