2014年5月12日月曜日

groovyとPushBulletを使用してモバイル機器に位置を送信する

groovyとPushBulletを使用してモバイル機器に位置を送信するには、以下のコードのように、addressパラメータにgoogle mapsのクエリーを設定します。
※API KEYとモバイル機器のdevice idenの取得の仕方はこちらを参照
groovyとPushBulletを使用してモバイル機器にメッセージを送信する
http://groovyarekore.blogspot.jp/2014/05/groovypushbullet.html

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.3')
import org.apache.http.client.entity.*
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.auth.*
import org.apache.http.message.*
import org.apache.http.protocol.*
import groovy.json.*

// api keyはAccount Settingsから参照可能
def apikey = "<API KEYをここにペースト>"
// device idenはAPIから取得可能
def device_iden = "<device idenをここにペースト>"

def httpclient = new DefaultHttpClient()
httpclient.getCredentialsProvider().setCredentials(
  new AuthScope("api.pushbullet.com", 443),
  new UsernamePasswordCredentials(apikey, ""));


def method = new HttpPost("https://api.pushbullet.com/api/pushes")
values = [
  new BasicNameValuePair("device_iden", device_iden),
  new BasicNameValuePair("type", "address"),
  new BasicNameValuePair("name", "東京駅"),
  new BasicNameValuePair("address", "https://www.google.co.jp/maps/@35.681382,139.766084,17z")
]
method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8))

response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
println json

0 件のコメント:

コメントを投稿