码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-03 20:44:58      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   for   sp   

一。在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()

标签:style   blog   http   io   color   ar   os   for   sp   

原文地址:http://www.cnblogs.com/chuiyuan/p/4071973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!