1. 程式人生 > >progressbar請求數據 加載demo1

progressbar請求數據 加載demo1

output run 標識 結果 message puts request set override

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/pb_handler1_loading"
        android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:visibility="invisible"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="getSubmit1" android:text
="GET Submit" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="getSubmit2" android:text="GET Submit2" /> <EditText android:id="@+id/et_handler1_result" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="顯示結果" > </EditText> </LinearLayout>
package com.example.volleylearn;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;


/**
 * 測試Handler的基本使用
 * @author 張曉飛
1. 創建Handler成員變量對象, 並重寫其handleMessage()
2. 在分/主線程創建Message對象
3. 使用handler對象發送Message
4. 在handleMessage()中處理消息
 */
public class HandlerTestActivity extends Activity {

    private ProgressBar pb_handler1_loading;
    private EditText et_handler1_result;
    
    //1. 創建Handler成員變量對象, 並重寫其handleMessage()
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {//在主線程執行
            if(msg.what == 1) {
                //4. 在handleMessage()中處理消息
                String result = (String) msg.obj;
                et_handler1_result.setText(result);
                pb_handler1_loading.setVisibility(View.INVISIBLE);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_test);

        pb_handler1_loading = (ProgressBar) findViewById(R.id.pb_handler1_loading);
        et_handler1_result = (EditText) findViewById(R.id.et_handler1_result);
    }

    /*
     1. 主線程, 顯示提示視圖(ProgressDialog/ProgressBar)
     2. 分線程, 聯網請求, 並得到響應數據
     3. 主線程, 顯示數據/隱藏提示視圖
     */
    public void getSubmit1(View v) {
        //1. 主線程, 顯示提示視圖(ProgressDialog/ProgressBar)
        pb_handler1_loading.setVisibility(View.VISIBLE);
        //2. 分線程, 聯網請求, 並得到響應數據
        new Thread(){
            public void run() {
                String path = "http://192.168.10.165:8080/Web_Server/index.jsp?name=Tom&age=12";
                try {
                    final String result = requestToString(path);
                    
                    //3. 主線程, 顯示數據
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            et_handler1_result.setText(result);
                            pb_handler1_loading.setVisibility(View.INVISIBLE);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    
    public void getSubmit2(View v) {
        //1). 主線程, 顯示提示視圖(ProgressDialog/ProgressBar)
        pb_handler1_loading.setVisibility(View.VISIBLE);
        //2). 分線程, 聯網請求, 並得到響應數據
        new Thread(){
            public void run() {
                String path = "http://192.168.10.165:8080/Web_Server/index.jsp?name=Tom&age=12";
                try {
                    String result = requestToString(path);
                    
                    //3). 主線程, 顯示數據
                    //2. 在分/主線程創建Message對象
                    Message message = Message.obtain();
                    message.what = 1;//標識
                    message.obj = result;
                    //3. 使用handler對象發送Message
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    
    /**
     * 請求服務器端, 得到返回的結果字符串
     * @param path  : http://192.168.30.165:8080/Web_server/index.jsp?username=tom&age=12
     * @return
     * @throws Exception
     */
    public String requestToString(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.connect();
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
        }
        baos.close();
        is.close();
        String result = baos.toString();
        connection.disconnect();
    
        return result;
    }

}

progressbar請求數據 加載demo1