Last active
December 12, 2015 04:39
-
-
Save smd877/4716154 to your computer and use it in GitHub Desktop.
Androidで利用できるxmlパーサー「simple」のサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.gr.java_conf.smd877.simplesample; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.app.Activity; | |
/** | |
* xmlパーサーSimpleを試すためのActivity | |
*/ | |
public class SimpleSampleActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_simple_sample); | |
} | |
/** | |
* Buttonボタンを押下した場合の処理 | |
* | |
* @param view | |
*/ | |
public void onClick(View view) { | |
// バックグラウンドで処理を開始するように要求 | |
AsyncTaskImpl task = new AsyncTaskImpl(this); | |
task.execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.gr.java_conf.smd877.simplesample; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import org.simpleframework.xml.Serializer; | |
import org.simpleframework.xml.core.Persister; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
/** | |
* AsyncTaskを継承したクラス.<BR> | |
* Google急上昇ワードランキングを取得してテキストを表示する。 | |
*/ | |
public class AsyncTaskImpl extends AsyncTask<Void, Void, ElementRtt> { | |
private Activity activity; | |
public AsyncTaskImpl(Activity activity) { | |
this.activity = activity; | |
} | |
@Override | |
protected ElementRtt doInBackground(Void... params) { | |
ElementRtt ret = null; | |
HttpURLConnection http = null; | |
InputStream in = null; | |
try { | |
// xmlをhttpで取得 | |
String strUrl = activity.getResources().getString(R.string.connect_url); | |
URL url = new URL(strUrl); | |
http = (HttpURLConnection)url.openConnection(); | |
http.connect(); | |
in = http.getInputStream(); | |
// ストリームからxmlを取得してオブジェクトにパースする | |
Serializer serializer = new Persister(); | |
ret = serializer.read(ElementRtt.class, in); | |
} | |
catch (Exception e) { | |
// 問題が発生した場合はnullをリターンする | |
ret = null; | |
} | |
finally { | |
try { | |
if (null != in) { | |
in.close(); | |
} | |
if (null != http) { | |
http.disconnect(); | |
} | |
} | |
catch (Exception e) { | |
} | |
} | |
return ret; | |
} | |
@Override | |
protected void onPostExecute(ElementRtt result) { | |
// パースに失敗したのでエラーをトースト表示 | |
if (null == result) { | |
Toast.makeText(activity, activity.getResources().getString(R.string.parse_error_message), | |
Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
// パースした結果をTextViewで表示する | |
TextView tv = (TextView)activity.findViewById(R.id.textView1); | |
StringBuffer sb = new StringBuffer(); | |
sb.append("Google急上昇ワードランキング\n"); | |
sb.append("update time : " + result.update + "\n\n"); | |
sb.append("Rank : Score : KeyWord\n"); | |
for (ElementItem item : result.item) { | |
sb.append(item.rank); | |
sb.append(" : "); | |
sb.append(item.dScore); | |
sb.append(" : "); | |
sb.append(item.query); | |
sb.append("\n"); | |
} | |
tv.setText(sb.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.gr.java_conf.smd877.simplesample; | |
import java.util.List; | |
import org.simpleframework.xml.Element; | |
import org.simpleframework.xml.ElementList; | |
import org.simpleframework.xml.Root; | |
/** | |
* xmlエレメントのBean(rtt) | |
*/ | |
@Root(name = "rtt", strict = false) | |
public class ElementRtt { | |
@Element | |
public String update; | |
@ElementList(inline = true) | |
public List<ElementItem> item; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.gr.java_conf.smd877.simplesample; | |
import org.simpleframework.xml.Element; | |
import org.simpleframework.xml.Root; | |
/** | |
* xmlエレメントのBean(item) | |
*/ | |
@Root(name = "item", strict = false) | |
public class ElementItem { | |
@Element | |
public String query; | |
@Element | |
public int rank; | |
@Element(name = "score") | |
public double dScore; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="jp.gr.java_conf.smd877.simplesample" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="8" /> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="jp.gr.java_conf.smd877.simplesample.SimpleSampleActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".SimpleSampleActivity" > | |
<TextView | |
android:id="@+id/textView1" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:text="TextView" /> | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:onClick="onClick" | |
android:text="Button" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">SimpleSample</string> | |
<string name="menu_settings">Settings</string> | |
<string name="connect_url">http://www.google.co.jp/m/services/trends/get</string> | |
<string name="parse_error_message">結果の取得に失敗しました</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment