首页 > 代码库 > 在WelcomeActivity中完成恢复用户功能 resumeUser()

在WelcomeActivity中完成恢复用户功能 resumeUser()

一。在WelcomeActivity中的代码 

这里我们是根据deviceid来查询用户的。

//要用到网络 ,所以开一个线程    private void resumeUser() {        ToastUtils.ToastShort(this, "resume user");        new Thread(){            @Override            public void run() {                        User mUser = new User();                mUser.setDeviceid(DeviceUtils.getDeviceId(WelcomeActivity.this));                User resumeUser =  mUser.resumeUser();                if (resumeUser!=null){                    WelcomeActivity.this.startActivity(new                             Intent(WelcomeActivity.this, MainActivity.class));                    WelcomeActivity.this.finish();                }            }            };    }    

 

 

二。在User类中的resumeUser()方法

/**     * resume user 用的是查询deviceid      * @return User      */    public User resumeUser() {        HttpUtils http = new HttpUtils();        User mUser = null;        Map<String , String>paramMap= new HashMap<String, String>();        paramMap.put("deviceid",getDeviceid() );        try{            String response = http.getExecute(paramMap, HttpGetUrl.getUser());            Log.i("User resumeUser() response",response);            mUser = new Gson().fromJson(response, User.class);            return mUser;        } catch (IOException e) {            e.printStackTrace();        }catch(Exception e2){            e2.printStackTrace();        }        return null;    }

 

 

三。在HttpUtils中要有getExecute(Map, String)方法

这里可以参考

http://ipjmc.iteye.com/blog/1577495

/**     * 不是static,不能直接引用,     * @ paramMap, url     * @throws IOException      * @throws ClientProtocolException      */    public  String getExecute(Map<String, String>paramMap, String url)             throws ClientProtocolException, IOException{        Log.i("getExecute" , "in");        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();                for (Map.Entry<String , String > param: paramMap.entrySet()){            String key = param.getKey();            params.add(new BasicNameValuePair(key,  param.getValue()));        }        //对对数编码         String param = URLEncodedUtils.format(params, "UTF-8");        HttpGet httpGet = new HttpGet(url+"?"+param);        HttpResponse response =  client.execute(httpGet);        return EntityUtils.toString(response.getEntity(),"utf-8");            }

 

在WelcomeActivity中完成恢复用户功能 resumeUser()