android Thread 数据加载 ProgressDialog 请求超时处理 线程 Handler

package com.qihoinfo;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import com.qihoinfo.util.ClientService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
	String requestByHttpGet = null;
	String requestByHttpPost = null;
	String returnResult ="";
	String url_str = "http://192.168.1.67/androidjson.php";
	Map<String, String> params = null;
	TextView textView;
	

	// 声明变量
	private ProgressDialog pd;
	// 定义Handler对象
	private Handler handler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0: 
				textView.setText(returnResult);
				break;
			default:
				textView.setText("请求严重超时,请检查你的网络设置是否通畅!");
				break;
			}
			// 只要执行到这里就关闭对话框
			pd.dismiss();
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.i("tag", "onCreate()-->" + Thread.currentThread().getName());

		textView = (TextView) findViewById(R.id.text);
		processThread();
		textView.setText(returnResult);

	}

	private void processThread() {
		// 构建一个下载进度条
		pd = ProgressDialog.show(MainActivity.this, "加载数据", "网络数据加载中......");
		Log.i("tag", "processThread()-->" + Thread.currentThread().getName());
		new Thread() {
			@Override
			public void run() {
				Log.i("tag", "run()-->" + Thread.currentThread().getName());
				
				//执行请求数据的方法
			    longTimeMethod();
			    
				if(returnResult.equals("")){
					// 执行完毕后给handler发送一个超时消息
					handler.sendEmptyMessage(1);
				}else{
					// 执行完毕后给handler发送一个空消息
					handler.sendEmptyMessage(0);
				}
			}
		}.start();

	}

	// 模拟下载文件的长耗时方法
	private void longTimeMethod() {
		try {
			Log.i("tag", "longTimeMethod-->" + Thread.currentThread().getName());
			Thread.sleep(0);
			params = new HashMap<String, String>();
			params.put("name", "张三");

			try {
				requestByHttpGet = ClientService.requestByHttpGet(url_str,
						params, "UTF-8");
				JSONArray jArray = new JSONArray(requestByHttpGet);
				if (jArray.length() > 0 && jArray != null) {
					for (int i = 0; i < jArray.length(); i++) {
						JSONObject json_data = jArray.getJSONObject(i);
						// 通过返回的json储存的搜索过得mysql列表值进行获取
						Log.e("json", json_data.toString());
						returnResult += "\n您的名称是 :"
								+ json_data.getString("uName") + "\nid:"
								+ json_data.getString("id");
					}
				}

				requestByHttpPost = ClientService.requestByHttpPost(url_str,
						params, "UTF-8");
				JSONArray jArray2 = new JSONArray(requestByHttpPost);
				for (int i = 0; i < jArray2.length(); i++) {
					JSONObject json_data = jArray2.getJSONObject(i);
					// 通过返回的json储存的搜索过得mysql列表值进行获取
					Log.e("json", json_data.toString());
					returnResult += "\n您的名称是 :" + json_data.getString("uName")
							+ "\nid:" + json_data.getString("id"); 
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(android,线程,handler,数据加载,请求超时处理)