1. 获取百度个人信息(使用Gson解析):
2. 代码案例:
(1)工程一览图,如下:
(2)activity_main.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.himi.oauthdemo.MainActivity" > 7 <Button 8 android:id="@+id/btn_oauth" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="Baidu OAuth" 12 android:onClick="onClickOAuth"/> 13 14 <TextView 15 android:id="@+id/tv_access_token" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="@string/hello_world" /> 19 20 <Button 21 android:id="@+id/btn_get_user_info" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="Baidu Get User Info" 25 android:onClick="onClickGetUserInfo"/> 26 27 <TextView 28 android:id="@+id/tv_result" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="@string/hello_world" /> 32 33 </LinearLayout>
布局效果,如下:
(3)MainActivity.java:
1 package com.jikexueyuan.oauth; 2 3 import java.io.IOException; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.text.TextUtils; 8 import android.view.Menu; 9 import android.view.MenuItem; 10 import android.view.View; 11 import android.widget.TextView; 12 import android.widget.Toast; 13 14 import com.baidu.api.AsyncBaiduRunner; 15 import com.baidu.api.AsyncBaiduRunner.RequestListener; 16 import com.baidu.api.Baidu; 17 import com.baidu.api.BaiduDialog.BaiduDialogListener; 18 import com.baidu.api.BaiduDialogError; 19 import com.baidu.api.BaiduException; 20 import com.google.gson.Gson; 21 import com.google.gson.annotations.SerializedName; 22 23 public class MainActivity extends Activity { 24 25 private TextView mTvAccessToken = null; 26 private TextView mTvResult = null; 27 private TextView mTvResultUser = null; 28 29 private Baidu mBaidu = null; 30 private Gson mGson = null; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 mBaidu = new Baidu("V3VW2ulGTGbTM32pXyAs7fvY", this); 36 mGson = new Gson(); 37 setContentView(R.layout.activity_main); 38 mTvAccessToken = (TextView)findViewById(R.id.tv_access_token); 39 mTvResult= (TextView)findViewById(R.id.tv_result); 40 mTvResultUser= (TextView)findViewById(R.id.tv_result_user); 41 } 42 43 public void OnClickOAuth(View v) { 44 mBaidu.authorize(this, true, true, new BaiduDialogListener() { 45 46 @Override 47 public void onError(BaiduDialogError arg0) { 48 refreshUI("error"); 49 } 50 51 @Override 52 public void onComplete(Bundle arg0) { 53 refreshUI(mBaidu.getAccessToken()); 54 } 55 56 @Override 57 public void onCancel() { 58 refreshUI("cancel"); 59 } 60 61 @Override 62 public void onBaiduException(BaiduException arg0) { 63 refreshUI("exception"); 64 } 65 }); 66 } 67 68 public void OnClickGetUserInfo(View v) { 69 String token = mBaidu.getAccessToken(); 70 if(TextUtils.isEmpty(token)) { 71 Toast.makeText(this, "Token is null", Toast.LENGTH_SHORT).show(); 72 } else { 73 AsyncBaiduRunner runner = new AsyncBaiduRunner(mBaidu); 74 String url = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo"; 75 runner.request(url, null, "GET", new RequestListener() { 76 77 @Override 78 public void onIOException(IOException arg0) { 79 refreshResultUI("onIOException"); 80 } 81 82 @Override 83 public void onComplete(String json) { 84 refreshResultUI(json); 85 } 86 87 @Override 88 public void onBaiduException(BaiduException arg0) { 89 refreshResultUI("onBaiduException"); 90 } 91 }); 92 /** 93 new Thread(){ 94 public void run() { 95 String url = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo"; 96 try { 97 final String jsonText = mBaidu.request(url, null, "GET"); 98 // final UserEntity user = mGson.fromJson(jsonText, UserEntity.class); 99 final UserEntity user = mGson.fromJson(jsonText, new TypeToken<UserEntity>(){}.getType()); 100 runOnUiThread(new Runnable() { 101 102 @Override 103 public void run() { 104 mTvResult.setText(jsonText); 105 mTvResultUser.setText(mGson.toJson(user)); 106 } 107 }); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } catch (BaiduException e) { 111 e.printStackTrace(); 112 } 113 } 114 }.start(); 115 **/ 116 } 117 } 118 119 120 private void refreshResultUI(final String msg) { 121 runOnUiThread(new Runnable() { 122 123 @Override 124 public void run() { 125 mTvResultUser.setText(msg); 126 } 127 }); 128 } 129 private void refreshUI(final String msg) { 130 runOnUiThread(new Runnable() { 131 132 @Override 133 public void run() { 134 mTvAccessToken.setText(msg); 135 } 136 }); 137 } 138 139 @Override 140 public boolean onCreateOptionsMenu(Menu menu) { 141 // Inflate the menu; this adds items to the action bar if it is present. 142 getMenuInflater().inflate(R.menu.main, menu); 143 return true; 144 } 145 146 @Override 147 public boolean onOptionsItemSelected(MenuItem item) { 148 // Handle action bar item clicks here. The action bar will 149 // automatically handle clicks on the Home/Up button, so long 150 // as you specify a parent activity in AndroidManifest.xml. 151 int id = item.getItemId(); 152 if (id == R.id.action_settings) { 153 return true; 154 } 155 return super.onOptionsItemSelected(item); 156 } 157 158 159 class UserEntity { 160 private String userid; 161 private String blood; 162 @SerializedName("username") 163 private String name; 164 public String getUserid() { 165 return userid; 166 } 167 public void setUserid(String userid) { 168 this.userid = userid; 169 } 170 public String getBlood() { 171 return blood; 172 } 173 public void setBlood(String blood) { 174 this.blood = blood; 175 } 176 public String getName() { 177 return name; 178 } 179 public void setName(String username) { 180 this.name = username; 181 } 182 183 } 184 }
与此同时AndroidMainfest.xml,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.jikexueyuan.oauth" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 <uses-permission android:name="android.permission.INTERNET" /> 11 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 12 13 <application 14 android:allowBackup="true" 15 android:icon="@drawable/ic_launcher" 16 android:label="@string/app_name" 17 android:theme="@style/AppTheme" > 18 <activity 19 android:name=".MainActivity" 20 android:label="@string/app_name" > 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 24 <category android:name="android.intent.category.LAUNCHER" /> 25 </intent-filter> 26 </activity> 27 </application> 28 29 </manifest>