【Android】Handler应用(二):从服务器端加载JSON数据的优化
2014-01-07 09:35
435 查看
在上一篇博客/article/1626189.html中,我们了解了Handler从服务器中加载JSON数据的过程。
为了实现代码的复用和进一步理解Handler的相关知识,我们对代码进行如下优化。
[java] view plaincopy
package com.app.myhandler;
import android.app.Activity;
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;
import com.app.util.MyThread;
public class HanderTest_Text_New extends Activity {
private Button button1, button2;
private TextView textView1, textView2;
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_new);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
new MyThread(handler,"http://10.0.2.2:8888/android/1.jsp",1).start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
new MyThread(handler,"http://10.0.2.2:8888/android/2.jsp",2).start();
}
});
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
textView1.setText(msg.obj.toString());
textView2.setText("文本二");
progressBar.setVisibility(View.GONE);
break;
case 2:
textView1.setText("文本一");
textView2.setText(msg.obj.toString());
progressBar.setVisibility(View.GONE);
break;
default:
break;
}
}
};
}
}
package com.app.util;
import java.util.Map;
import android.os.Handler;
import android.os.Message;
public class MyThread extends Thread{
private Handler handler;
private String url;
private int what;
public MyThread(Handler handler, String url, int what) {
this.handler = handler;
this.url = url;
this.what = what;
}
@Override
public void run() {
// TODO Auto-generated method stub
String result = ApplicationDemo.handleGet(url);
Message message = handler.obtainMessage();
message.what = what;
message.obj = result;
//向handler发送消息
handler.sendMessage(message);
}
}
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="加载一" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="加载二" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本一"
android:textColor="#E7473E"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本二"
android:textColor="#4DB849"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>
加载二
为了实现代码的复用和进一步理解Handler的相关知识,我们对代码进行如下优化。
Activity文件
HanderTest_Text_New.java[java] view plaincopy
package com.app.myhandler;
import android.app.Activity;
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;
import com.app.util.MyThread;
public class HanderTest_Text_New extends Activity {
private Button button1, button2;
private TextView textView1, textView2;
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_new);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
new MyThread(handler,"http://10.0.2.2:8888/android/1.jsp",1).start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
new MyThread(handler,"http://10.0.2.2:8888/android/2.jsp",2).start();
}
});
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
textView1.setText(msg.obj.toString());
textView2.setText("文本二");
progressBar.setVisibility(View.GONE);
break;
case 2:
textView1.setText("文本一");
textView2.setText(msg.obj.toString());
progressBar.setVisibility(View.GONE);
break;
default:
break;
}
}
};
}
}
MyThread文件
[java] view plaincopypackage com.app.util;
import java.util.Map;
import android.os.Handler;
import android.os.Message;
public class MyThread extends Thread{
private Handler handler;
private String url;
private int what;
public MyThread(Handler handler, String url, int what) {
this.handler = handler;
this.url = url;
this.what = what;
}
@Override
public void run() {
// TODO Auto-generated method stub
String result = ApplicationDemo.handleGet(url);
Message message = handler.obtainMessage();
message.what = what;
message.obj = result;
//向handler发送消息
handler.sendMessage(message);
}
}
XML布局文件
[html] view plaincopy<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="加载一" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="加载二" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本一"
android:textColor="#E7473E"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本二"
android:textColor="#4DB849"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>
效果图
加载一加载二
相关文章推荐
- 【Android】Handler的应用(二):从服务器端加载JSON数据的优化
- 【Android】Handler应用(二):从服务器端加载JSON数据的优化
- 【Android】Handler应用(一):从服务器端加载JSON数据
- 【Android】Handler的应用(一):从服务器端加载JSON数据
- Handler应用(一):从服务器端加载JSON数据
- 【Android】Handler应用(三):从服务器端分页加载更新ListView
- 【Android】Handler应用(三):从服务器端分页加载更新ListView
- ArcGIS Android应用客户端加载JSON格式业务数据
- ArcGIS Android应用客户端加载JSON格式业务数据
- 【Android】Handler的应用(三):从服务器端分页加载更新ListView
- Android 实现Json数据解析,并进行应用!
- android-async-http开源项目对服务器端返回JSON数据的处理
- android客户端从服务器端获取json数据并解析的实现代码
- android客户端从服务器端获取json数据并解析的实现代码(重要)
- Android中关于Volley的使用(二)加载Json数据
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- 网络加载数据和解析JSON格式数据案例之空气质量监测应用
- Android实战简易教程-第六十六枪(服务器端搭建和服务器端Json数据交互)
- Android客户端从服务器端获取json数据并解析的实现代码
- Android客户端与服务器端的json数据交互(很详细)