PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/development/samples/WiktionarySimple/src/com/example/android/simplewiktionary/SimpleWikiHelper.java

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Java | 214 lines | 99 code | 26 blank | 89 comment | 6 complexity | 142bfc6f42ec1f178342d01024a7e8cb MD5 | raw file
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.android.simplewiktionary;
  17. import org.apache.http.HttpEntity;
  18. import org.apache.http.HttpResponse;
  19. import org.apache.http.StatusLine;
  20. import org.apache.http.client.HttpClient;
  21. import org.apache.http.client.methods.HttpGet;
  22. import org.apache.http.impl.client.DefaultHttpClient;
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.json.JSONObject;
  26. import android.content.Context;
  27. import android.content.pm.PackageInfo;
  28. import android.content.pm.PackageManager;
  29. import android.content.pm.PackageManager.NameNotFoundException;
  30. import android.net.Uri;
  31. import android.util.Log;
  32. import java.io.ByteArrayOutputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. /**
  36. * Helper methods to simplify talking with and parsing responses from a
  37. * lightweight Wiktionary API. Before making any requests, you should call
  38. * {@link #prepareUserAgent(Context)} to generate a User-Agent string based on
  39. * your application package name and version.
  40. */
  41. public class SimpleWikiHelper {
  42. private static final String TAG = "SimpleWikiHelper";
  43. /**
  44. * Regular expression that splits "Word of the day" entry into word
  45. * name, word type, and the first description bullet point.
  46. */
  47. public static final String WORD_OF_DAY_REGEX =
  48. "(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}";
  49. /**
  50. * Partial URL to use when requesting the detailed entry for a specific
  51. * Wiktionary page. Use {@link String#format(String, Object...)} to insert
  52. * the desired page title after escaping it as needed.
  53. */
  54. private static final String WIKTIONARY_PAGE =
  55. "http://en.wiktionary.org/w/api.php?action=query&prop=revisions&titles=%s&" +
  56. "rvprop=content&format=json%s";
  57. /**
  58. * Partial URL to append to {@link #WIKTIONARY_PAGE} when you want to expand
  59. * any templates found on the requested page. This is useful when browsing
  60. * full entries, but may use more network bandwidth.
  61. */
  62. private static final String WIKTIONARY_EXPAND_TEMPLATES =
  63. "&rvexpandtemplates=true";
  64. /**
  65. * {@link StatusLine} HTTP status code when no server error has occurred.
  66. */
  67. private static final int HTTP_STATUS_OK = 200;
  68. /**
  69. * Shared buffer used by {@link #getUrlContent(String)} when reading results
  70. * from an API request.
  71. */
  72. private static byte[] sBuffer = new byte[512];
  73. /**
  74. * User-agent string to use when making requests. Should be filled using
  75. * {@link #prepareUserAgent(Context)} before making any other calls.
  76. */
  77. private static String sUserAgent = null;
  78. /**
  79. * Thrown when there were problems contacting the remote API server, either
  80. * because of a network error, or the server returned a bad status code.
  81. */
  82. public static class ApiException extends Exception {
  83. public ApiException(String detailMessage, Throwable throwable) {
  84. super(detailMessage, throwable);
  85. }
  86. public ApiException(String detailMessage) {
  87. super(detailMessage);
  88. }
  89. }
  90. /**
  91. * Thrown when there were problems parsing the response to an API call,
  92. * either because the response was empty, or it was malformed.
  93. */
  94. public static class ParseException extends Exception {
  95. public ParseException(String detailMessage, Throwable throwable) {
  96. super(detailMessage, throwable);
  97. }
  98. }
  99. /**
  100. * Prepare the internal User-Agent string for use. This requires a
  101. * {@link Context} to pull the package name and version number for this
  102. * application.
  103. */
  104. public static void prepareUserAgent(Context context) {
  105. try {
  106. // Read package name and version number from manifest
  107. PackageManager manager = context.getPackageManager();
  108. PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
  109. sUserAgent = String.format(context.getString(R.string.template_user_agent),
  110. info.packageName, info.versionName);
  111. } catch(NameNotFoundException e) {
  112. Log.e(TAG, "Couldn't find package information in PackageManager", e);
  113. }
  114. }
  115. /**
  116. * Read and return the content for a specific Wiktionary page. This makes a
  117. * lightweight API call, and trims out just the page content returned.
  118. * Because this call blocks until results are available, it should not be
  119. * run from a UI thread.
  120. *
  121. * @param title The exact title of the Wiktionary page requested.
  122. * @param expandTemplates If true, expand any wiki templates found.
  123. * @return Exact content of page.
  124. * @throws ApiException If any connection or server error occurs.
  125. * @throws ParseException If there are problems parsing the response.
  126. */
  127. public static String getPageContent(String title, boolean expandTemplates)
  128. throws ApiException, ParseException {
  129. // Encode page title and expand templates if requested
  130. String encodedTitle = Uri.encode(title);
  131. String expandClause = expandTemplates ? WIKTIONARY_EXPAND_TEMPLATES : "";
  132. // Query the API for content
  133. String content = getUrlContent(String.format(WIKTIONARY_PAGE,
  134. encodedTitle, expandClause));
  135. try {
  136. // Drill into the JSON response to find the content body
  137. JSONObject response = new JSONObject(content);
  138. JSONObject query = response.getJSONObject("query");
  139. JSONObject pages = query.getJSONObject("pages");
  140. JSONObject page = pages.getJSONObject((String) pages.keys().next());
  141. JSONArray revisions = page.getJSONArray("revisions");
  142. JSONObject revision = revisions.getJSONObject(0);
  143. return revision.getString("*");
  144. } catch (JSONException e) {
  145. throw new ParseException("Problem parsing API response", e);
  146. }
  147. }
  148. /**
  149. * Pull the raw text content of the given URL. This call blocks until the
  150. * operation has completed, and is synchronized because it uses a shared
  151. * buffer {@link #sBuffer}.
  152. *
  153. * @param url The exact URL to request.
  154. * @return The raw content returned by the server.
  155. * @throws ApiException If any connection or server error occurs.
  156. */
  157. protected static synchronized String getUrlContent(String url) throws ApiException {
  158. if (sUserAgent == null) {
  159. throw new ApiException("User-Agent string must be prepared");
  160. }
  161. // Create client and set our specific user-agent string
  162. HttpClient client = new DefaultHttpClient();
  163. HttpGet request = new HttpGet(url);
  164. request.setHeader("User-Agent", sUserAgent);
  165. try {
  166. HttpResponse response = client.execute(request);
  167. // Check if server response is valid
  168. StatusLine status = response.getStatusLine();
  169. if (status.getStatusCode() != HTTP_STATUS_OK) {
  170. throw new ApiException("Invalid response from server: " +
  171. status.toString());
  172. }
  173. // Pull content stream from response
  174. HttpEntity entity = response.getEntity();
  175. InputStream inputStream = entity.getContent();
  176. ByteArrayOutputStream content = new ByteArrayOutputStream();
  177. // Read response into a buffered stream
  178. int readBytes = 0;
  179. while ((readBytes = inputStream.read(sBuffer)) != -1) {
  180. content.write(sBuffer, 0, readBytes);
  181. }
  182. // Return result from buffered stream
  183. return new String(content.toByteArray());
  184. } catch (IOException e) {
  185. throw new ApiException("Problem communicating with API", e);
  186. }
  187. }
  188. }