程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> handler-xutils框架的httputils怎麼實現每隔5秒請求一次服務器?

handler-xutils框架的httputils怎麼實現每隔5秒請求一次服務器?

編輯:編程綜合問答
xutils框架的httputils怎麼實現每隔5秒請求一次服務器?

xutils框架的httputils怎麼實現每隔5秒請求一次服務器?
代碼如下每隔5秒執行一次getdataFromNet不出錯如何實現

 public class MainActivity extends Activity {
    private Thread thread;
    private MapView mapView;
    private AMap aMap;
    private LocationManagerProxy locationManagerProxy;
    private ImageView iv_maptag;
    private EditText et_search;
    private ImageView iv_user;
    private ImageView iv_setmenu;
    private ImageView iv_location;
    private Marker marker;
    private OnLocationChangedListener mListener;
    private MarkerOptions markerOptions;
    private ArrayList<Aircraft> aircraftList;
    private MarkerOptions markerOptionslist;
    private ArrayList<Marker> listmarker;
    private ArrayList<MarkerOptions> list = new ArrayList<MarkerOptions>();
    private String line = null;
    private Handler mHandler = new Handler();
    private boolean ThreadFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        mapView.onCreate(savedInstanceState);
        setUpMap();
        initData();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                // list=new ArrayList<MarkerOptions>();
                aircraftList = (ArrayList<Aircraft>) msg.obj;
                if (aircraftList != null) {
                    for (int i = 0; i < aircraftList.size(); i++) {
                        Double latitedu = Double.parseDouble(aircraftList
                                .get(i).Latitude);
                        Double longitedu = Double.parseDouble(aircraftList
                                .get(i).Longitude);
                        markerOptionslist = new MarkerOptions();
                        markerOptionslist.position(new LatLng(latitedu,
                                longitedu));
                        // Toast.makeText(getApplicationContext(),
                        // markerOptionslist+"", Toast.LENGTH_LONG).show();
                        markerOptionslist.perspective(true);// 設置marker的近大遠小效果
                        markerOptionslist
                                .icon(BitmapDescriptorFactory
                                        .fromResource(R.drawable.civil_aircraft_selected));
                        markerOptionslist.draggable(true);
                        markerOptionslist.setFlat(true);
                        markerOptionslist.period(50);// 定位marker圖標在中間顯示
                        list.add(markerOptionslist);
                    }
                    listmarker = aMap.addMarkers(list, false);
                }
                break;
            default:
                break;
            }
        }
    };

    private void initData() {
        getDataFromNet();// 每隔五秒執行一次此方法不報錯
        // requestFlyAirplane();//第一種實現報錯,異常為java.lang.RuntimeException//Can't create handler inside thread that has not called Looper.prepare()

        // new Thread(networkTask1).start();//第二種一樣報錯

    }

    private void initView() {
        mapView = (MapView) findViewById(R.id.main_mapview1);
        iv_location = (ImageView) findViewById(R.id.iv_location);
        iv_setmenu = (ImageView) findViewById(R.id.iv_setmenu);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    private void setUpMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
    // private void requestFlyAirplane(){
    // thread=new Thread(new Runnable() {
    // @Override
    // public void run() {
    // while(ThreadFlag){
    // try {
    // aircraftList1=getDataFromNet();
    // Message message=handler.obtainMessage();
    // message.obj=aircraftList;
    // message.what=1;
    // handler.sendMessage(message);
    // // getDataFromNet();
    // // Thread.sleep(5000);
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }
    // }
    // });
    // thread.start();
    // }
    Runnable networkTask1 = new Runnable() {
        @Override
        public void run() {
            // TODO
            while (ThreadFlag) {
                try {
                    aircraftList = getDataFromNet();
                    Message message = handler.obtainMessage();
                    message.obj = aircraftList;
                    message.what = 1;
                    handler.sendMessage(message);
                    // getDataFromNet();
                    // Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    ArrayList<Aircraft> aircraftList1 = new ArrayList<AircraftInfo.Aircraft>();
    private ArrayList<Aircraft> getDataFromNet() {
        HttpUtils httpUtils = new HttpUtils();
        String url = "http://124.207.158.74:60023/aircraft/baidu";
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json; encoding=utf-8");
        params.setHeader("Accept", "application/json");
        params.setHeader("Username", "haifeng");
        params.setHeader("appkey", "fff2dd6003243e46a4aefab71d4366f0c3ebfb12");
        params.setHeader("system", "1");
        params.setHeader("logindevice",
                Secure.getString(getContentResolver(), Secure.ANDROID_ID));
        httpUtils.send(HttpMethod.GET, url, params,
                new RequestCallBack<String>() {
                    // 請求成功
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        // Toast.makeText(getApplicationContext(),
                        // "請求成功"+result, Toast.LENGTH_SHORT).show();
                        // processData(result);
                        Gson gson = new Gson();
                        AircraftInfo aircrafts = gson.fromJson(result,
                                AircraftInfo.class);
                        aircraftList1 = aircrafts.AircraftInfos;
                        Message message = handler.obtainMessage();
                        message.obj = aircraftList;
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                    // 請求失敗
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Toast.makeText(getApplicationContext(), "網絡錯誤",
                                Toast.LENGTH_SHORT).show();
                        error.printStackTrace();
                    }
                });
        try {
            // httpUtils.wait(5000);
            // httpUtils.notifyAll();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return aircraftList1;
    }

}

最佳回答:


你看看這個,希望對你有幫助http://www.tuicool.com/articles/nMFb2q

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved