QQWEB登陆 出现的子线程取数据的问题解决方法
package com.example.qqaccreditweibo;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class QqWeiboLoginActivity extends Activity {
// QQ返回的网页地址
private static final String QQ_BACK_URI = "http://qzs.qq.com/open/connect/widget/mobile/login/proxy.htm";
// 通过uri请求
private static final String AUTHORIZE = "https://graph.qq.com/oauth2.0/authorize";
// APP_ID 填写自己的appid
private static final String CLIENT_ID = "";
// 后端跳转限制
private static final String REDIRECT_URI = "http://okchang.com/";
// 用于截取
private static final String openid_tag = "?#&openid=";
private static final String appid_tag = "&appid=";
private static final String access_tag = "&access_token=";
private static final String USER_INFO="https://graph.qq.com/user/get_user_info";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qqweibologin_activity);
//加载WebView
WebView view=(WebView) findViewById(R.id.qq_weibo_login_view);
//加载javaScript
view.getSettings().setJavaScriptEnabled(true);
//请求
view.loadUrl(AUTHORIZE+"?response_type=token&client_id="+CLIENT_ID+"&redirect_uri="+REDIRECT_URI);
//内部
view.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return false;
}
});
QqWeiboLogin(view);
}
private void QqWeiboLogin(WebView view){
view.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url)
{
String newStr = url.substring(1,url.indexOf(".com"));
if(null !=newStr){
if(QQ_BACK_URI.contains(newStr)){
new Thread(new Runnable() {
@Override
public void run() {
String openid = url.substring(url.indexOf(openid_tag)+openid_tag.length(),url.indexOf("&appid="));
String appid = url.substring(url.indexOf(appid_tag)+appid_tag.length(),url.indexOf("&access_token="));
String access_token = url.substring(url.indexOf(access_tag)+access_tag.length(),url.indexOf("&key="));
String serverURL = USER_INFO+"?oauth_consumer_key="+appid+"&access_token="+access_token+"&openid="+openid+"&format=json";
try {
HttpGet httpRequest = new HttpGet(serverURL);// 建立http get联机
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求
if (httpResponse.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串
onJson(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
view.loadUrl(url);
return true;
}
});
}
private void onJson(String json){
QqLoginUserBean bean=new QqLoginUserBean();
JSONObject array=null;
try {
array=new JSONObject(json);
} catch (Exception e) {
}
if(null !=array){
bean.setRet(array.optString("ret"));
bean.setMsg(array.optString("msg"));
bean.setIs_lost(array.optString("is_lost"));
bean.setCity(array.optString("city"));
bean.setFigureurl(array.optString("figureurl"));
bean.setFigureurl_1(array.optString("figureurl_1"));
bean.setFigureurl_2(array.optString("figureurl_2"));
bean.setFigureurl_qq_1(array.optString("figureurl_qq_1"));
bean.setFigureurl_qq_2(array.optString("figureurl_qq_2"));
bean.setGender(array.optString("gender"));
bean.setIs_yellow_vip(array.optString("is_yellow_vip"));
bean.setLevel(array.optString("level"));
bean.setNickname(array.optString("nickname"));
bean.setProvince(array.optString("province"));
bean.setVip(array.optString("vip"));
bean.setYear(array.optString("year"));
bean.setIs_yellow_year_vip(array.optString("is_yellow_year_vip"));
bean.setYellow_vip_level(array.optString("yellow_vip_level"));
//线程切换
if(null!=QqWebLoginListener.mHttpListener){
QqWebLoginListener.mHttpListener.returnJson(bean);
}
}
this.finish();
}
}
package com.example.qqaccreditweibo;
import android.content.Context;
import android.content.Intent;
public class QqWebLoginListener {
public interface WebLoginListener {
public void returnJson(QqLoginUserBean result);
};
protected static WebLoginListener mHttpListener;
public static void loginOnTententWeb(Context context, WebLoginListener listenter) {
mHttpListener = listenter;
Intent intent=new Intent(context, QqWeiboLoginActivity.class);
context.startActivity(intent);
}
}
package com.example.qqaccreditweibo;
public class QqLoginUserBean {
private String ret;
private String msg;
private String is_lost;
private String nickname;
private String gender;
private String province;
private String city;
private String year;
private String figureurl;
private String figureurl_1;
private String figureurl_2;
private String figureurl_qq_1;
private String figureurl_qq_2;
private String is_yellow_vip;
private String vip;
private String yellow_vip_level;
private String level;
private String is_yellow_year_vip;
public String getRet() {
return ret;
}
public void setRet(String ret) {
this.ret = ret;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getIs_lost() {
return is_lost;
}
public void setIs_lost(String is_lost) {
this.is_lost = is_lost;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getFigureurl() {
return figureurl;
}
public void setFigureurl(String figureurl) {
this.figureurl = figureurl;
}
public String getFigureurl_1() {
return figureurl_1;
}
public void setFigureurl_1(String figureurl_1) {
this.figureurl_1 = figureurl_1;
}
public String getFigureurl_2() {
return figureurl_2;
}
public void setFigureurl_2(String figureurl_2) {
this.figureurl_2 = figureurl_2;
}
public String getFigureurl_qq_1() {
return figureurl_qq_1;
}
public void setFigureurl_qq_1(String figureurl_qq_1) {
this.figureurl_qq_1 = figureurl_qq_1;
}
public String getFigureurl_qq_2() {
return figureurl_qq_2;
}
public void setFigureurl_qq_2(String figureurl_qq_2) {
this.figureurl_qq_2 = figureurl_qq_2;
}
public String getIs_yellow_vip() {
return is_yellow_vip;
}
public void setIs_yellow_vip(String is_yellow_vip) {
this.is_yellow_vip = is_yellow_vip;
}
public String getVip() {
return vip;
}
public void setVip(String vip) {
this.vip = vip;
}
public String getYellow_vip_level() {
return yellow_vip_level;
}
public void setYellow_vip_level(String yellow_vip_level) {
this.yellow_vip_level = yellow_vip_level;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getIs_yellow_year_vip() {
return is_yellow_year_vip;
}
public void setIs_yellow_year_vip(String is_yellow_year_vip) {
this.is_yellow_year_vip = is_yellow_year_vip;
}
}
package com.example.qqaccreditweibo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.example.qqaccreditweibo.QqWebLoginListener.WebLoginListener;
public class MainActivity extends Activity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
QqWebLoginListener.loginOnTententWeb(this,new WebLoginListener() {
@Override
public void returnJson(QqLoginUserBean result) {
Log.e("tag","result"+result.getCity());
}
});
}
}