package com.xdd.tenp;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People.Phones;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
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 com.xdd.tenp.enity.Uite;
/**
*
* 通过调用Tencent类的login函数发起登录/校验登录态。
* 该API具有两个作用:
* (1)如果开发者没有调用mTencent实例的setOpenId、setAccessToken API,
* 则该API执行正常的登录操作;
* (2)如果开发者先调用mTencent实例的setOpenId、setAccessToken
* API,则该API执行校验登录态的操作。如果登录态有效,则返回成功给应用,
* 如果登录态失效,则会自动进入登录流程,将最新的登录态数据返回给应用
* @author super bear
*
*/
public class ActivityLogin extends Activity {
private static final String APPID = "100302037";
private static final String TAG = "ActivityLogin";
private EditText et1;
private EditText et2;
private Tencent mTencent;
private IUiListener loginListener;
private IUiListener userInfoListener;
private String scope = "all";
private UserInfo userInfo;
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRequestQueue = Volley.newRequestQueue(this);
Uite.getMac(this);
setupViews();
initData();
onLoginGuDong();
Cursor localCursor = key(“content://cn.ledongli.ldl.DataProvider/StatsManager/dailystatskey/1.4521824E9”);
String str = "";
String paramDate = str;
if (localCursor != null)
{
paramDate = str;
if (localCursor.moveToFirst()) {
paramDate = localCursor.getString(localCursor.getColumnIndex("dailystats"));
}
}
System.out.println("paramDate="+paramDate);
}
public Cursor key(String paramString)
{
Uri param = Uri.parse(paramString);
return ActivityLogin.this.getContentResolver().query(param, null, null, null, null);
}
@Override
protected void onDestroy() {
if (mTencent != null) {
mTencent.logout(ActivityLogin.this);
}
super.onDestroy();
}
/**
* 获取View
*/
private void setupViews() {
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("你点击了使用qq登录按钮");
if (!mTencent.isSessionValid()) {
mTencent.login(ActivityLogin.this, scope, loginListener);
}
}
});
findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("开始获取用户信息");
if(mTencent.getQQToken() == null){
System.out.println("qqtoken == null");
}
userInfo = new UserInfo(ActivityLogin.this, mTencent.getQQToken());
userInfo.getUserInfo(userInfoListener);
}
});
}
/**
* 加载数据
*/
private void initData() {
mTencent = Tencent.createInstance(APPID, ActivityLogin.this);
//要所有权限,不用再次申请增量权限,这里不要设置成 get_user_info,add_t
scope = "all";
loginListener = new IUiListener() {
@Override
public void onError(UiError arg0) {
}
// {
// "ret": 0,
// "pay_token": "D1B5A68200A59EF682EEBCFA8CE74022",
// "pf": "desktop_m_qq-10000144-android-2002-",
// "query_authority_cost": 437,
// "authority_cost": -44496979,
// "openid": "1CF5D212DF1194C97A797B7597B3B03F",
// "expires_in": 7776000,
// "pfkey": "7b36c06639b070b6a35061874981a095",
// "msg": "",
// "access_token": "F3A7F1EE28AEDD520CDB1B16C25129E9",
// "login_cost": 536
// }
@Override
public void onComplete(Object value) {
System.out.println("有数据返回..");
if (value == null) return;
try {
Toast.makeText(ActivityLogin.this, "登录成功",Toast.LENGTH_LONG).show();
JSONObject jo = (JSONObject) value;
String msg = jo.getString("msg");
System.out.println("json=" + String.valueOf(jo));
System.out.println("msg="+msg);
// if ("sucess".equals(msg)) {
String openID = jo.getString("openid");
String accessToken = jo.getString("access_token");
String expires = jo.getString("expires_in");
mTencent.setOpenId(openID);
mTencent.setAccessToken(accessToken, expires);
// }
} catch (Exception e) {
}
}
@Override
public void onCancel() {
}
};
userInfoListener = new IUiListener() {
@Override
public void onError(UiError arg0) {
}
// {
// "is_yellow_year_vip": "0",
// "ret": 0,
// "figureurl_qq_1": "http://q.qlogo.cn/qqapp/100302037/1CF5D212DF1194C97A797B7597B3B03F/40",
// "figureurl_qq_2": "http://q.qlogo.cn/qqapp/100302037/1CF5D212DF1194C97A797B7597B3B03F/100",
// "nickname": "楼不在高 有鬼则名",
// "yellow_vip_level": "0",
// "is_lost": 0,
// "msg": "",
// "city": "冲绳",
// "figureurl_1": "http://qzapp.qlogo.cn/qzapp/100302037/1CF5D212DF1194C97A797B7597B3B03F/50",
// "vip": "0",
// "level": "0",
// "figureurl_2": "http://qzapp.qlogo.cn/qzapp/100302037/1CF5D212DF1194C97A797B7597B3B03F/100",
// "province": "",
// "is_yellow_vip": "0",
// "gender": "男",
// "figureurl": "http://qzapp.qlogo.cn/qzapp/100302037/1CF5D212DF1194C97A797B7597B3B03F/30"
// }
@Override
public void onComplete(Object arg0) {
if(arg0 == null) return;
try {
JSONObject jo = (JSONObject) arg0;
int ret = jo.getInt("ret");
System.out.println("json=" + String.valueOf(jo));
if(ret == 100030){
//权限不够,需要增量授权
Runnable r = new Runnable() {
public void run() {
mTencent.reAuth(ActivityLogin.this, "all", new IUiListener() {
@Override
public void onError(UiError arg0) {
}
@Override
public void onComplete(Object arg0) {
}
@Override
public void onCancel() {
}
});
}
};
ActivityLogin.this.runOnUiThread(r);
}else {
String nickName = jo.getString("nickname");
String gender = jo.getString("gender");
Toast.makeText(ActivityLogin.this, "你好," + nickName, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
@Override
public void onCancel() {
}
};
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_API) {
if (resultCode == Constants.RESULT_LOGIN) {
Tencent.handleResultData(data, loginListener);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onLoginGuDong(){
// {
// "access_token": "3fd3337fb3da44204896cf89e7836ffc",
// "expire_in": 93312000,
// "token_type": "bearer",
// "user_id": "c45dd8cf-4244-4ed1-9711-ec3710220031",
// "refresh_token": "16fbc52a3887ff322b96fb7e2f126af8"
// }
// http://api.codoon.com/external_token_with_info?token=F3A7F1EE28AEDD520CDB1B16C25129E9&external_user_id=1CF5D212DF1194C97A797B7597B3B03F&client_id=dc039f07e003da02938a5bc4605b5acc&source=qq2
String url ="http://api.codoon.com/external_token_with_info?token=F3A7F1EE28AEDD520CDB1B16C25129E9&external_user_id=1CF5D212DF1194C97A797B7597B3B03F&client_id=dc039f07e003da02938a5bc4605b5acc&source=qq2";
AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
//RequestParams params = new RequestParams(); // 创建请求参数的封装的对象
client.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.addHeader("Content-Type", "application/x-www-form-urlencoded");
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
String GetData = null;
GetData = new String(arg2);
Log.e("ok_GetData", GetData);
try {
JSONObject jsonObject = new JSONObject(GetData);
String access_token = jsonObject.getString("access_token");
String refresh_token= jsonObject.getString("refresh_token");
Log.e("ok_access_token="+access_token, refresh_token);
onGetWXurl(access_token);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
Log.e("on_GetData","" +arg0);
Toast.makeText(ActivityLogin.this, "连接网络错误", Toast.LENGTH_SHORT).show();
}
});
}
public void onGetWXurl(String access_token){
// {
// "status": "OK",
// "data": {
// "qrticket": "http://we.qq.com/d/AQCIc9a340sXVa6sYC7vdDk3bu3EW7WBOTNFSg-_",
// "deviceid": "gh_877fdb038ace_63cc410908c157b2b21f506d9c644200"
// },
// "description": ""
// }
// http://api.codoon.com/api/get_device_qrcode
// Authorization: Bearer 3fd3337fb3da44204896cf89e7836ffc
String url ="http://api.codoon.com/api/get_device_qrcode";
AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
client.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.addHeader("Content-Type", "application/x-www-form-urlencoded");
client.addHeader("Authorization", "Bearer " + access_token);
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
String GetData = null;
GetData = new String(arg2);
Log.e("ok_GetData", GetData);
try {
JSONObject jsonObject = new JSONObject(GetData).getJSONObject("data");
String qrticket = jsonObject.getString("qrticket");
String deviceid= jsonObject.getString("deviceid");
Log.e("ok_ qrticket="+qrticket, deviceid);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
Log.e("on_GetData","" +arg0);
Toast.makeText(ActivityLogin.this, "连接网络错误", Toast.LENGTH_SHORT).show();
}
});
}
public void onStepsUpload(String access_token){
String url ="ttp://api.codoon.com/api/mobile_steps_upload_detail";
AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
RequestParams params = new RequestParams(); // 创建请求参数的封装的对象
client.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.addHeader("Content-Type", "application/x-www-form-urlencoded");
client.addHeader("Authorization", "Bearer " + access_token);
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
String GetData = null;
GetData = new String(arg2);
Log.e("ok_GetData", GetData);
try {
JSONObject jsonObject = new JSONObject(GetData).getJSONObject("data");
String qrticket = jsonObject.getString("qrticket");
String deviceid= jsonObject.getString("deviceid");
Log.e("ok_ qrticket="+qrticket, deviceid);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
Log.e("on_GetData","" +arg0);
Toast.makeText(ActivityLogin.this, "连接网络错误", Toast.LENGTH_SHORT).show();
}
});
// ***************************************
// {"date":"2015-10-30","content":[######]}
// ["19:20",1000,0.8870400230026246,15.84,20],
// “[” + 随机时间 () + “,” + TempStrps + “,” + 时间_取随机时间戳 () + “,” + 随机数 () + “,” + 随机数 () + “]”
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
Map<String, String> map = new HashMap<String, String>();
map.put("date", "value1");
map.put("content", "value2");
JSONObject jsonObject = new JSONObject(map);
JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "response -> " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage(), error);
}
})
{
//注意此处override的getParams()方法,在此处设置post需要提交的参数根本不起作用
//必须象上面那样,构成JSONObject当做实参传入JsonObjectRequest对象里
//所以这个方法在此处是不需要的
// @Override
// protected Map<String, String> getParams() {
// Map<String, String> map = new HashMap<String, String>();
// map.put("name1", "value1");
// map.put("name2", "value2");
// return params;
// }
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json; charset=UTF-8");
return headers;
}
};
requestQueue.add(jsonRequest);
}
public void ldl(View V){
// http://blog.sina.com.cn/s/blog_7263950101017l79.html
Cursor localCursor = key(“content://cn.ledongli.ldl.DataProvider/StatsManager/dailystatskey/1.4521824E9”);
String str = "";
String paramDate = str;
if (localCursor != null)
{
paramDate = str;
if (localCursor.moveToFirst()) {
paramDate = localCursor.getString(localCursor.getColumnIndex("dailystats"));
}
}
System.out.println("paramDate="+paramDate);
while (localCursor.moveToNext()) {
for(int i=0;i<localCursor.getCount();i++){
String name = localCursor.getString(i);
Log.i("System.out", name);
}
}
startActivity(new Intent(this, ActivityMain.class));
}
}