My favorites
▼
|
Sign in
adakoda-android-sample
Android sample
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
SetsudenYahooAPISample
/
src
/
com
/
adakoda
/
android
/
yahoo
/
setsuden
/
SetsudenClient.java
r61
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.adakoda.android.yahoo.setsuden;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
public class SetsudenClient {
// http://developer.yahoo.co.jp/webapi/shinsai/setsuden/v1/latestpowerusage.html
private static final String REQUEST_URL = "http://setsuden.yahooapis.jp/v1/Setsuden/latestPowerUsage";
private static final String PARAM_KEY_APPID = "appid";
private static final String PARAM_KEY_OUTPUT = "output";
private static final String PARAM_VALUE_OUTPUT_JSON = "json";
private static final String ELECTRIC_POWER_USAGE = "ElectricPowerUsage"; // String
private static final String AREA = "Area"; // String
private static final String USAGE = "Usage"; // Object
private static final String CAPACITY = "Capacity"; // Object
private static final String DATE = "Date"; // String
private static final String HOUR = "Hour"; // String
// for Usage and Capacity
private static final String UNIT = "@unit"; // String
private static final String VALUE = "$"; // int
private String mAppID;
public SetsudenClient(String appID) {
mAppID = appID;
}
public LatestPowerUsageResult getLatestPowerUsage() {
LatestPowerUsageResult result = new LatestPowerUsageResult();
// Build uri
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.path(REQUEST_URL);
uriBuilder.appendQueryParameter(PARAM_KEY_APPID, mAppID);
uriBuilder.appendQueryParameter(PARAM_KEY_OUTPUT, PARAM_VALUE_OUTPUT_JSON);
String uri = Uri.decode(uriBuilder.build().toString());
// Request HTTP GET
HttpUriRequest httpGet = new HttpGet(uri);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
try {
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
String stringEntity = EntityUtils.toString(httpResponse.getEntity());
// Parse JSON result
JSONObject jsonEntity = new JSONObject(stringEntity);
if (jsonEntity != null) {
JSONObject jsonElectricPowerUsage =
jsonEntity.optJSONObject(ELECTRIC_POWER_USAGE);
if (jsonElectricPowerUsage != null) {
result.setArea(jsonElectricPowerUsage.optString(AREA));
JSONObject jsonUsage =
jsonElectricPowerUsage.optJSONObject(USAGE);
if (jsonUsage != null) {
result.setUsageUnit(jsonUsage.optString(UNIT));
result.setUsageValue(jsonUsage.optInt(VALUE));
}
JSONObject jsonCapacity =
jsonElectricPowerUsage.optJSONObject(CAPACITY);
if (jsonCapacity != null) {
result.setCapacityUnit(jsonCapacity.optString(UNIT));
result.setCapacityValue(jsonCapacity.optInt(VALUE));
}
result.setDate(jsonElectricPowerUsage.optString(DATE));
result.setHour(jsonElectricPowerUsage.optString(HOUR));
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}
Show details
Hide details
Change log
r41
by adakoda.jamjampan on Mar 29, 2011
Diff
[No log message]
Go to:
...etsudenYahooAPISample/.classpath
.../SetsudenYahooAPISample/.project
...hooAPISample/AndroidManifest.xml
...nk/SetsudenYahooAPISample/assets
...ahooAPISample/default.properties
...sudenYahooAPISample/proguard.cfg
/trunk/SetsudenYahooAPISample/res
...YahooAPISample/res/drawable-hdpi
...ample/res/drawable-hdpi/icon.png
...YahooAPISample/res/drawable-ldpi
...ample/res/drawable-ldpi/icon.png
...YahooAPISample/res/drawable-mdpi
...ample/res/drawable-mdpi/icon.png
...etsudenYahooAPISample/res/layout
...hooAPISample/res/layout/main.xml
...etsudenYahooAPISample/res/values
...APISample/res/values/strings.xml
/trunk/SetsudenYahooAPISample/src
...k/SetsudenYahooAPISample/src/com
...enYahooAPISample/src/com/adakoda
...PISample/src/com/adakoda/android
...a/android/setsudenyahooapisample
...sudenYahooAPISampleActivity.java
...le/src/com/adakoda/android/yahoo
...m/adakoda/android/yahoo/setsuden
...uden/LatestPowerUsageResult.java
...hoo/setsuden/SetsudenClient.java
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 3302 bytes, 87 lines
View raw file
File properties
svn:mime-type
text/plain
Powered by
Google Project Hosting