1、Handler类,主要用于复杂的通信,但操作相对复杂
public class MainActivity extends Activity { private static final int SET_TEXT = 1; private static final int ERROR = 2; private EditText metPath, metShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } Handler handler = new Handler() { public void handleMessage(Message msg) { String result = (String) msg.obj; switch (msg.what) { case SET_TEXT: metShow.setText(result); break; case ERROR: Toast.makeText(MainActivity.this, "操作失败", Toast.LENGTH_LONG).show(); break; } }; }; private void setListener() { findViewById(R.id.btnComfire).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new ChildThread().start(); } }); } private void initView() { metPath = (EditText) findViewById(R.id.evPath); metShow = (EditText) findViewById(R.id.etShow); } /** * 连接网络属于耗时操作,放到子线程中进行相应操作 * @author CHJ * */ class ChildThread extends Thread { public void run() { String path = metPath.getText().toString().trim(); try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求参数, 这里为了简单只设置两种 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (200 == code) {// 建立连接成功 // 获取网页源代码并且转换成字符串 InputStream is = conn.getInputStream(); String result = new UtilTools().streamToString(is); if (result == null) { new UtilTools().sendInfoToMainThread(handler, ERROR,null); } else { new UtilTools().sendInfoToMainThread(handler, SET_TEXT,result); } } else { new UtilTools().sendInfoToMainThread(handler, ERROR, null); } } catch (Exception e) { e.printStackTrace(); new UtilTools().sendInfoToMainThread(handler, ERROR, null); } }; } } class UtilTools { /** * 将一个输出流转换成字符串 * @param is * @return 字符串 */ public String streamToString(InputStream is) { // 在这个方法中还可以处理乱码,这里就不操作了。思路是通过解析 // 网页的源码,可以得到网页的编码格式,然后进行相应操作 BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String result = null; try { String temp = null; while ((temp = br.readLine()) != null) { sb.append(temp); } result = sb.toString().trim(); is.close(); return result; } catch (Exception e) { e.printStackTrace(); try { is.close(); } catch (IOException e1) { e1.printStackTrace(); } return null; } } /** * 往主线程发送消息,这里主要是考虑代码的复用 * @param handler */ public void sendInfoToMainThread(Handler handler, int tag, String obj) { Message msg = new Message(); msg.what = tag; msg.obj = obj; handler.sendMessage(msg); } }
2、runOnUiThread()主要用于简单的通信,操作简单。
runOnUiThread(new Runnable() {
public void run() {
// 操作主线程的UI
}
});
public class MainActivity extends Activity { private EditText metPath, metShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { findViewById(R.id.btnComfire).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread() { public void run() { String path = metPath.getText().toString().trim(); try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求参数, 这里为了简单只设置两种 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (200 == code) {// 建立连接成功 // 获取网页源代码并且转换成字符串 InputStream is = conn.getInputStream(); final String result = new UtilTools() .streamToString(is); if (result == null) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"操作失败", Toast.LENGTH_LONG).show(); } }); } else { runOnUiThread(new Runnable() { public void run() { metShow.setText(result); } }); } } else { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"操作失败", Toast.LENGTH_LONG).show(); } }); } } catch (Exception e) { e.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"操作失败", Toast.LENGTH_LONG).show(); } }); } }; }.start(); } }); } private void initView() { metPath = (EditText) findViewById(R.id.evPath); metShow = (EditText) findViewById(R.id.etShow); } } class UtilTools { /** * 将一个输出流转换成字符串 * * @param is * @return 字符串 */ public String streamToString(InputStream is) { // 在这个方法中还可以处理乱码,这里就不操作了。思路是通过解析 // 网页的源码,可以得到网页的编码格式,然后进行相应操作 BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String result = null; try { String temp = null; while ((temp = br.readLine()) != null) { sb.append(temp); } result = sb.toString().trim(); is.close(); return result; } catch (Exception e) { e.printStackTrace(); try { is.close(); } catch (IOException e1) { e1.printStackTrace(); } return null; } } }
本文详细介绍了在Android应用中如何利用Handler类和runOnUiThread方法进行耗时网络请求的操作,并同步更新UI界面,确保用户体验流畅。
387

被折叠的 条评论
为什么被折叠?



