日期:2014-05-16  浏览次数:20355 次

【Android】Handler的应用(一):从服务器端加载JSON数据

最终目的

以JSON的形式,将数据存入服务器端。
在Android中,以Handler加载显示大批量文字。
在此以加载金庸小说《天龙八部(新修版)》为例(2580480 字节)。

以tomcat为服务器,在jsp中以I/O读取本机上的txt文件,写入JSON数据。

在加载过程中,以进度条的形式提示用户需要等待。
加载完成后,进度条消失,并显示加载内容。

Activity文件

package com.app.test02;

import java.util.Map;

import com.app.util.MyApplication;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HanderTest_Text extends Activity {
	private Button button;
	private TextView textView;
	private Handler handler;
	private ProgressBar progressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hander_text);

		button = (Button) findViewById(R.id.button1);
		textView = (TextView) findViewById(R.id.textView1);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressBar.setVisibility(View.VISIBLE);
				button.setText("加载中...");
				new MyThread().start();
			}
		});
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				textView.setText(msg.obj.toString());
				progressBar.setVisibility(View.GONE);
				button.setText("加载完成");
				button.setEnabled(false);
			}
		};
	}
	
	class MyThread extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String result = ApplicationDemo.handleGet("http://10.0.2.2:8888/android/");
			Map map = ApplicationDemo.getMap(result);
			
			Message message = handler.obtainMessage();
			message.obj = map.get("book");
			try {
				sleep(30000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			handler.sendMessage(message);
		}
	}
}

XML布局文件

activity_hander_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fff" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:gravity="center">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载文本" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
                <TextView
			        android:id="@+id/textView1"
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="" 
			        android:textColor="#000"/>
            
        </LinearL