Android开发日记(三)

 1 protected void onCreate(Bundle savedInstanceState) {
 2         super.onCreate(savedInstanceState);
 3         setContentView(R.layout.adshow);
 4         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 5         intent = getIntent();
 6         map = (HashMap<String, Object>) intent.getSerializableExtra("map");
 7         getPostDetailsViaNewThread();
 8     }
 9 
10 private void getPostDetailsViaNewThread() {
11 
12         // TODO Auto-generated method stub
13         new Thread(new Runnable() {
14             @Override
15             public void run() {
16                 try {
17                     postdetailsJsonArray = postService.getPostDetails("5826");
18                     if (postdetailsJsonArray != null) {
19                         handler.sendEmptyMessage(0);
20                     } else {
21                         return;
22                     }
23                 } catch (Exception e) {
24                     // TODO: handle exception
25                     e.printStackTrace();
26                 }
27             }
28         }).start();
29     }
30 private Handler handler = new Handler() {
31         @Override
32         // 当有消息发送出来的时候就执行Handler的这个方法
33         public void handleMessage(Message msg) {
34             super.handleMessage(msg);
35             initView();
36         }
37     };
38 private void initView(){
39     //postdetailsJsonArray = postService.getPostDetails("5826");
40     JSONObject firstJsonObject = postdetailsJsonArray.optJSONObject(0);
41     TextView textView_JobTitle=(TextView)findViewById(R.id.textView_JobTitle);
42     TextView textView_JobRelease=(TextView)findViewById(R.id.textView_JobRelease);
43     TextView textView_JobDetail = (TextView) findViewById(R.id.textView_JobDetail);
44     TextView TextView_JobTime = (TextView) findViewById(R.id.TextView_JobTime);
45     //JobTitle=map.get("jobTitle").toString();
46     //JobTime=map.get("jobTime").toString();
47     //JobDetail = map.get("jobDetail").toString();
48     //JobRelease = map.get("releasetime").toString();
49     textView_JobTitle.setText(firstJsonObject.optString("posttitle"));
50     textView_JobRelease.setText(firstJsonObject.optString("date"));
51     //textView_JobDetail.setText(JobDetail);
52     //TextView_JobTime.setText(JobTime);
53 }
View Code

要实现从服务器端数据库获取数据写到Android端的过程:

先调用onCreate()方法打开XML,

在调用getPostDetailsViaNewThread()线程

使用postService中定义的方法getPostDetails("5826")获得服务端传过来的JSON数组对象

postdetailsJsonArray = postService.getPostDetails("5826");

转到Handler()方法,在其中调用initView();

JSONObject firstJsonObject = postdetailsJsonArray.optJSONObject(0);定义JSON对象

initView()中用textView_JobTitle.setText(firstJsonObject.optString("posttitle"));把JSON对象写到XML对应的控件中。

 

posted @ 2015-12-05 12:50  Wei_java  阅读(366)  评论(0编辑  收藏  举报