其實我的應用程序正在使用LinkedIn登錄通過linkedin應用程序和web視圖。通過LinkedIn登錄工作正常,但如果在LinkedIn應用程序不可用的情況下,我正在使用webview來處理登錄。這也可以正常工作,但今天它突然停留在一些代碼異常行中。我在我的Linkedin對話框類中獲得了空訪問令牌,所以我喜歡嘗試捕獲,但之後它會顯示空白的webview我不知道該怎麼做知道。這裏是我的LinkedIn的對話框代碼陷入LinkedIn通過Webview登錄
public class LinkedinDialog extends Dialog
{
private ProgressDialog progressDialog = null;
public static LinkedInApiClientFactory factory;
public static LinkedInOAuthService oAuthService;
public static LinkedInRequestToken liToken;
private WebView mWebView;
private Context mContext;
public LinkedinDialog(Context context, ProgressDialog progressDialog)
{
super(context);
mContext = context;
this.progressDialog = progressDialog;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);// must call before super.
super.onCreate(savedInstanceState);
setContentView(R.layout.ln_dialog);
setWebView();
}
/**
* set webview.
*/
private void setWebView()
{
mWebView = (WebView) findViewById(R.id.webkitWebView1);
mWebView.getSettings().setJavaScriptEnabled(true);
try {
LinkedinDialog.oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(AppConstants.LINKEDIN_CONSUMER_KEY, AppConstants.LINKEDIN_CONSUMER_SECRET);
LinkedinDialog.factory = LinkedInApiClientFactory.newInstance(AppConstants.LINKEDIN_CONSUMER_KEY, AppConstants.LINKEDIN_CONSUMER_SECRET);
LinkedinDialog.liToken = LinkedinDialog.oAuthService.getOAuthRequestToken(AppConstants.OAUTH_CALLBACK_URL);
mWebView.loadUrl(LinkedinDialog.liToken.getAuthorizationUrl());
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setPictureListener(new PictureListener() {
@Override
public void onNewPicture(WebView view, Picture picture) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
}catch (ExceptionInInitializerError e){
AppLogs.printLogs("ExceptionInInitializerError :: " , " err ::");
LinkedinDialog.this.dismiss();
mWebView.goBack();
}catch (NoClassDefFoundError ex){
AppLogs.printLogs("NoClassDefFoundError :: " , " err ::");
ex.printStackTrace();
LinkedinDialog.this.dismiss();
mWebView.goBack();
}catch (Exception ee){
LinkedinDialog.this.dismiss();
mWebView.goBack();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
cancel();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
class HelloWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains(AppConstants.OAUTH_CALLBACK_URL))
{
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
cancel();
for (OnVerifyListener d : listeners)
{
// call listener method
d.onVerify(verifier);
}
}
else if (url.contains("https://www.google.co.in/"))
{
cancel();
}
else
{
Log.e("LinkedinSample", "url: " + url);
view.loadUrl(url);
}
return true;
}
}
/**
* List of listener.
*/
private List<OnVerifyListener> listeners = new ArrayList<OnVerifyListener>();
/**
* Register a callback to be invoked when authentication have finished.
*
* @param data
* The callback that will run
*/
public void setVerifierListener(OnVerifyListener data)
{
listeners.add(data);
}
/**
* Listener for oauth_verifier.
*/
public interface OnVerifyListener
{
/**
* invoked when authentication have finished.
*
* @param verifier
* oauth_verifier code.
*/
public void onVerify(String verifier);
}
}
這是函數調用的登錄按鈕
private void linkedInLogin()
{
mProgressDialog = new ProgressDialog(LoginActivity.this);
mProgressDialog.setMessage(AppConstants.sLoadingMsg);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
d = new LinkedinDialog(LoginActivity.this,mProgressDialog);
d.show();
d.setVerifierListener(new LinkedinDialog.OnVerifyListener() {
@SuppressLint("NewApi")
@Override
public void onVerify(String verifier) {
try {
accessToken = LinkedinDialog.oAuthService.getOAuthAccessToken(LinkedinDialog.liToken, verifier);
LinkedinDialog.factory.createLinkedInApiClient(accessToken);
client = factory.createLinkedInApiClient(accessToken);
AppLogs.printLogs("LinkedinSample", "ln_access_token: " + accessToken.getToken());
mAccessTokenLogin = accessToken.getToken();
com.google.code.linkedinapi.schema.Person p = null;
p = client.getProfileForCurrentUser(EnumSet.of(
ProfileField.ID, ProfileField.FIRST_NAME,
ProfileField.LAST_NAME, ProfileField.HEADLINE,
ProfileField.SUMMARY, ProfileField.PUBLIC_PROFILE_URL,
ProfileField.INDUSTRY, ProfileField.PICTURE_URL,
ProfileField.LOCATION, ProfileField.LOCATION_NAME, ProfileField.EMAIL_ADDRESS));
AppLogs.printLogs("linkedin id"," :: "+p.getId());
UserLinkedInData userLinkedInData = UserLinkedInData.getInstance();
try {
if (p.getEmailAddress() == null | p.getEmailAddress().isEmpty()) {
userLinkedInData.setmEmailAddress(AppConstants.sNotAvailable);
} else {
userLinkedInData.setmEmailAddress(p.getEmailAddress());
}
}catch (Exception e){
userLinkedInData.setmEmailAddress(AppConstants.sNotAvailable);
}
try {
if (!p.getFirstName().isEmpty()) {
userLinkedInData.setmFirstName(p.getFirstName());
} else {
userLinkedInData.setmFirstName(AppConstants.sNotAvailable);
}
}catch (Exception e){
userLinkedInData.setmFirstName(AppConstants.sNotAvailable);
}
}
}
});
mProgressDialog.dismiss();
}
所有jar依賴關係:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile project(':linkedin-sdk')
compile files('libs/linkedin-j-android.jar')
compile files('libs/commons-codec-1.3.jar')
compile files('libs/signpost-core-1.2.1.1.jar')
compile files('libs/signpost-commonshttp4-1.2.1.2.jar')
compile files('libs/signpost-jetty6-1.2.1.1.jar')
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:22.0.0'
compile 'com.googlecode.libphonenumber:libphonenumber:7.0.4'
compile "com.google.android.gms:play-services:8.1.0"
compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
compile 'com.android.support:multidex:1.0.0'}
異常我在第一次點擊獲得:
W/System.err﹕ java.lang.ExceptionInInitializerError
W/System.err﹕ at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthProvider(LinkedInOAuthServiceImpl.java:230)
W/System.err﹕ at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:170)
W/System.err﹕ at com.app.util.LinkedinDialog.setWebView(LinkedinDialog.java:64)
W/System.err﹕ at com.app.util.LinkedinDialog.onCreate(LinkedinDialog.java:51)
W/System.err﹕ at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
W/System.err﹕ at android.app.Dialog.show(Dialog.java:274)
W/System.err﹕ at com.app.circles.LoginActivity.linkedInLogin(LoginActivity.java:228)
W/System.err﹕ at com.app.circles.LoginActivity.access$100(LoginActivity.java:72)
W/System.err﹕ at com.app.circles.LoginActivity$1.onClick(LoginActivity.java:158)
然後ip RESS後退按鈕重新登錄,然後獲得新的例外是這樣的:
W/System.err﹕ java.lang.NoClassDefFoundError: com.google.code.linkedinapi.client.constant.LinkedInApiUrls
你有可能發佈錯誤日誌或堆棧跟蹤? – jagmohan
@jagmohan請檢查我更新的問題 –
我解決了這個問題,我自己...我只清除構建包裝,然後我刪除linkedin的現有jar文件,如linkedin-j,路標核心,公共編解碼器,並再次導入相同的文件然後建立項目......現在它運行良好。 –