Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android JSON解析示例代碼

Android JSON解析示例代碼

編輯:高級開發

來自Google官方的有關android平台的JSON解析示例,如果遠程服務器使用了JSon而不是XML的數據提供,在android平台上已經內置的org.JSon包可以很方便的實現手機客戶端的解析處理。下面Android123一起分析下這個例子,幫助android開發者需要有關 HTTP通訊、正則表達式、JSON解析、appWidget開發的一些知識。

public class WordWidget extends AppWidgetProvider { //appWidget
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {

context.startService(new Intent(context, UpdateService.class)); //避免ANR,所以Widget中開了個服務
}

public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateVIEws = buildUpdate(this);

ComponentName thisWidget = new ComponentName(this, WordWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateVIEws);
}


public RemoteVIEws buildUpdate(Context context) {
// Pick out month names from resources
Resources res = context.getResources();
String[] monthNames = res.getStringArray(R.array.month_names);

Time today = new Time();
today.setToNow();


String pageName = res.getString(R.string.template_wotd_title,
monthNames[today.month], today.monthDay);
RemoteViews updateVIEws = null;
String pageContent = "";

try {

SimpleWikiHelper.prepareUserAgent(context);
pageContent = SimpleWikiHelper.getPageContent(pageName, false);
} catch (ApIException e) {
Log.e("WordWidget", "Couldn't contact API", e);
} catch (ParseException e) {
Log.e("WordWidget", "Couldn't parse API response", e);
}


Pattern pattern = Pattern.compile(SimpleWikiHelper.Word_OF_DAY_REGEX); //正則表達式處理,有關定義見下面的SimpleWikiHelper類
Matcher matcher = pattern.matcher(pageContent);
if (matcher.find()) {

updateViews = new RemoteVIEws(context.getPackageName(), R.layout.widget_Word);

String WordTitle = matcher.group(1);
updateViews.setTextVIEwText(R.id.word_title, WordTitle);
updateViews.setTextVIEwText(R.id.Word_type, matcher.group(2));
updateViews.setTextVIEwText(R.id.definition, matcher.group(3).trim());


String definePage = res.getString(R.string.template_define_url,
Uri.encode(WordTitle));
Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage)); //這裡是打開相應的網頁,所以Uri是http的url,action是vIEw即打開web浏覽器
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, defineIntent, 0 /* no flags */);
updateVIEws.setOnClickPendingIntent(R.id.widget, pendingIntent); //單擊Widget打開Activity

} else {

updateViews = new RemoteVIEws(context.getPackageName(), R.layout.widget_message);
CharSequence errorMessage = context.getText(R.string.widget_error);
updateViews.setTextVIEwText(R.id.message, errorMessage);
}
return updateVIEws;
}

@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}

有關網絡通訊的實體類,以及一些常量定義如下:

public class SimpleWikiHelper {
private static final String TAG = "SimpleWikiHelper";

public static final String Word_OF_DAY_REGEX =
"(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}";

private static final String WIKTIONARY_PAGE =
"http://en.wiktionary.org/w/api.PHP?action=query&prop=revisions&titles=%s&" +
"rvprop=content&format=JSon%s";

private static final String WIKTIONARY_EXPAND_TEMPLATES =
"&rvexpandtemplates=true";

private static final int HTTP_STATUS_OK = 200;


private static byte[] sBuffer = new byte[512];


private static String sUserAgent = null;

public static class ApIException extends Exception {
public ApIException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}

public ApIException(String detailMessage) {
super(detailMessage);
}
}


public static class ParseException extends Exception {
public ParseException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}
}

public static void prepareUserAgent(Context context) {
try {
// Read package name and version number from manifest
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
sUserAgent = String.format(context.getString(R.string.template_user_agent),
info.packageName, info.versionName);

} catch(NameNotFoundException e) {
Log.e(TAG, "Couldn't find package information in PackageManager", e);
}
}


public static String getPageContent(String title, boolean expandTemplates)
throws ApIException, ParseException {

String encodedTitle = Uri.encode(title);
String expandClause = expandTemplates ? WIKTIONARY_EXPAND_TEMPLATES : "";


String content = getUrlContent(String.format(WIKTIONARY_PAGE, encodedTitle, expandClause));
try {

JSONObject response = new JSONObject(content);
JSONObject query = response.getJSONObject("query");
JSONObject pages = query.getJSONObject("pages");
JSONObject page = pages.getJSONObject((String) pages.keys().next());
JSONArray revisions = page.getJSONArray("revisions");
JSONObject revision = revisions.getJSONObject(0);
return revision.getString("*");
} catch (JSONException e) {
throw new ParseException("Problem parsing API response", e);
}
}


protected static synchronized String getUrlContent(String url) throws ApIException {
if (sUserAgent == null) {
throw new ApIException("User-Agent string must be prepared");
}


HttpClient client = new DefaultHttpClIEnt();
HttpGet request = new HttpGet(url);
request.setHeader("User-Agent", sUserAgent); //設置客戶端標識

try {
HttpResponse response = clIEnt.execute(request);

StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HTTP_STATUS_OK) {
throw new ApIException("Invalid response from server: " +
status.toString());
}

HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent(); //獲取HTTP返回的數據流

ByteArrayOutputStream content = new ByteArrayOutputStream();

int readBytes = 0;
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes); //轉化為字節數組流
}

return new String(content.toByteArray()); //從字節數組構建String
} catch (IOException e) {
throw new ApIException("Problem communicating with API", e);
}
}
}

有關整個每日維基的widget例子比較簡單,主要是幫助大家積累常用代碼,了解android平台 JSON的處理方式,畢竟很多Server還是Java的。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved