创建子线程通过http获取数据后,如何让数据显示在UI上,显然,直接在子线程中修改是不可以的,移至线程外,通过成员变量修改也是不行的,通过学习,尝试,发现handler能实现这一功能。通过handler接收子线程获取的数据,并修改UI;子线程通过handler把数据往外传,具体代码如下:
package com.belle.apptest;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.belle.db.Record;
import com.belle.http.HttpUtil;
import com.belle.tools.PubUtil;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author yangyu 功能描述:第二种实现方式,Activity实现方式
*/
public class QueryTradeActivity extends Activity {
// 定义标题栏上的按钮
private TextView name,sfz,sbkh;
private Button ret;
private String mesg = "";
private String sfzh = "";
private String cjxm = "";
private String cjsfz="";
private String kid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
setContentView(R.layout.activity_message);
Log.v("test", "come");
name = (TextView) findViewById(R.id.name);
sfz = (TextView) findViewById(R.id.sfz);
sbkh = (TextView) findViewById(R.id.sbkh);
ret = (Button) findViewById(R.id.ret);
sfzh = PubUtil.searchSFZ(getApplicationContext()).getSfz();
Log.i("sfzh",sfzh);
MyHandler mhandler = new MyHandler(name,sfz,sbkh);
MyThread mthread = new MyThread(mhandler);
new Thread(mthread).start();
ret.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(QueryTradeActivity.this, MainActivity.class);
startActivity(intent);
QueryTradeActivity.this.finish();
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
});
}
public class MyHandler extends Handler{
private TextView name;
private TextView sfz;
private TextView sbkh;
public MyHandler(TextView tv1,TextView tv2,TextView tv3){
this.name = tv1;
this.sfz = tv2;
this.sbkh = tv3;
}
public void handleMessage(Message msg){
super.handleMessage(msg);
Bundle bundle = msg.getData();
Log.i("bundle",bundle.toString());
HashMap map = PubUtil.annalyzeJSON(bundle.getString("personInfo"));
name.setText(PubUtil.nullToEmpty(map.get("cjxm")+""));
sfz.setText(PubUtil.nullToEmpty(map.get("cjsfz")+""));
sbkh.setText(PubUtil.nullToEmpty(map.get("kid")+""));
}
}
public class MyThread implements Runnable{
MyHandler handler;
public MyThread(MyHandler handler){
this.handler = handler;
}
public void run(){
try{
String url = HttpUtil.BASE_URL + "queryMessageServlet";
mesg = HttpUtil.queryStringForPost(url,sfzh);
Log.i("mesg",mesg);
}catch(Exception e){
Log.i("Register",e.toString());
}
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("personInfo", mesg);
msg.setData(bundle);
handler.sendMessage(msg);
}
}
}