用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

侧滑+QQ登录

2017-02-05 作者: cwm1996举报

[android]代码库

package com.bwie.demo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.bwie.bean.Bean;
import com.bwie.demo.view.XListView;
import com.google.gson.Gson;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Tencent mTencent;
    private String APP_ID = "1105924500";
    private IUiListener loginListener;
    private IUiListener userInfoIUiListener;
    private XListView xListView;
    private int num = 1;
    private String tag = "csh";
    List<Bean.ResultBean.ListBean> listBean = new ArrayList<>();
    Myadapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.btn);
        xListView = (XListView) findViewById(R.id.xlistview);
        xListView.setPullLoadEnable(true);
        SlidingMenu slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        // 设置滑动菜单视图的宽度
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.fragment2);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Fragment1()).commit();
        initlogin();
        //登陆按钮监听
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击登陆", Toast.LENGTH_SHORT).show();
                mTencent.login(MainActivity.this, "all", loginListener);
            }
        });
        //初始化数据
        getdate();
        //上拉加载下拉刷新监听
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                tag = "onRefresh";
                num = 5;
                getdate();
            }
            @Override
            public void onLoadMore() {
                tag
                        = "onLoadMore";
                num += 1;
                getdate();
            }
        });
    }
    //请求数据
    public void getdate() {
        String path = "http://v.juhe.cn/weixin/query?key=484057f2721591bc4730f6b370c7ff31&pno" + num;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //成功返回数据
                String result = responseInfo.result;
                Bean bean = new Gson().fromJson(result, Bean.class);
                List<Bean.ResultBean.ListBean> list = bean.getResult().getList();
                //判断是初始化还是上拉加载下拉刷新
                if (tag.equals("csh")) {
                    listBean.addAll(list);
                    myadapter = new Myadapter(MainActivity.this, listBean);
                    xListView.setAdapter(myadapter);
                } else if (tag.equals("onRefresh")) {
                    listBean.clear();
                    listBean.addAll(list);
                    myadapter.notifyDataSetChanged();
                    xListView.stopRefresh();
                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
                    xListView.setRefreshTime(format.format(new Date()));
                } else if (tag.equals("onLoadMore")) {
                    listBean.addAll(list);
                    myadapter.notifyDataSetChanged();
                    xListView.stopLoadMore();
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }
    //初始化登陆方法
    public void initlogin() {

        mTencent = Tencent.createInstance(APP_ID, MainActivity.this);

        loginListener = new IUiListener() {
            @Override
            public void onComplete(Object o) {

                Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                JSONObject jo = (JSONObject) o;
                String openID;

                try {
                    openID = jo.getString("openid");
                    String accessToken = jo.getString("access_token");
                    String expires_in = jo.getString("expires_in");
                    mTencent.setOpenId(openID);
                    mTencent.setAccessToken(accessToken, expires_in);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        };
        userInfoIUiListener = new IUiListener() {
            @Override
            public void onComplete(Object o) {
                JSONObject jo = (JSONObject) o;
                try {
                    //赋值
                    String nickname = jo.getString("nickname");
                    String figureurl_qq_2 = jo.getString("figureurl_qq_2");
                    new BitmapUtils(MainActivity.this).display(Fragment1.imageView, figureurl_qq_2);
                    Fragment1.textView.setText(nickname);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        };
    }

    //获取用户信息方法
    private void getUserInfo() {


        UserInfo info = new UserInfo(this, mTencent.getQQToken());
        info.getUserInfo(userInfoIUiListener);
    }

    //回调方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
        if (requestCode == Constants.REQUEST_LOGIN) {

            Tencent.handleResultData(data, loginListener);
            getUserInfo();
        }
    }
}
///////////////////////////////////////////////////////
<activity
            android:name="com.tencent.tauth.AuthActivity"
            android:noHistory="true"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tencent1105924500" />
            </intent-filter>
        </activity>
        <activity android:name="com.tencent.connect.common.AssistActivity"
                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:configChanges="orientation|keyboardHidden|screenSize"
            />


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...