android程式設計實例入門 -Sample改寫分享 (2014/11/19)

android程式設計實例入門 -Sample改寫分享 (2014/11/19)

android程式設計實例入門 -Sample改寫分享 (2014/11/19)

 

此範例為程式碼(11\Sample04)的改寫,建立建立EditText*1+Button*1+ListView*1來實作GUI,利用HttpClient抓取網路上XML+ XmlPullParserFactory和XmlPullParser來分析XML,程式碼如下所示:

 

package com.jashsample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Camera.Size;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
/*
 * 建立EditText*1+Button*1+ListView*1,利用HttpClient抓取網路上XML+ XmlPullParserFactory和XmlPullParser來分析XML。
 */
/*
 * <uses-permission android:name="android.permission.INTERNET" />
 */
public class MainActivity extends Activity {
EditText et;
Button bt;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
et = new EditText(this);
bt = new Button(this);
lv = new ListView(this);
bt.setText("讀取");
ll.addView(et);
ll.addView(bt);
ll.addView(lv);
bt.setOnClickListener(new SampleClickListener());
}
class SampleClickListener implements OnClickListener
{
public void onClick(View v)
{
try
{
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
String uri = et.getText().toString();
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet();
hg.setURI(new URI(uri));
HttpResponse hr = hc.execute(hg);
InputStream is = hr.getEntity().getContent();
BufferedReader br = new  BufferedReader(new InputStreamReader(is));
XmlPullParserFactory xpf = XmlPullParserFactory.newInstance();
XmlPullParser xpp = xpf.newPullParser();
xpp.setInput(br);
int type = 0;
type = xpp.getEventType();
HashMap<String,String> mp = null;
String title = "";
String description = "";
boolean isItem = false;
while (type != XmlPullParser.END_DOCUMENT)
{
if(type == XmlPullParser.START_TAG)
{
if(xpp.getName().equals("item"))
{
mp = new HashMap<String, String>();
isItem = true;
}
else if(xpp.getName().equals("title") )
{
if(isItem == true)
{
title = xpp.nextText();
mp.put("title", title);
}
}
else if(xpp.getName().equals("description"))
{
if(isItem == true)
{
description = xpp.nextText();
mp.put("description", description);
}
}
}
else if(type == XmlPullParser.END_TAG)
{
if(xpp.getName().equals("item"))
{
list.add(mp);
isItem = false;
}
}
type = xpp.next();
}
String[] keys = {"title", "description"};
int[] ids = {android.R.id.text1, android.R.id.text2};
SimpleAdapter sa = new SimpleAdapter
(getApplicationContext(), list, android.R.layout.simple_list_item_2, keys, ids);
lv.setAdapter(sa);
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "請確認URL。", Toast.LENGTH_LONG).show();
}
}
}
}



 



發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *