VerifyError&找不到类twitter4j.TwitterFactory错误

时间:2013-11-11 07:39:38

标签: android twitter4j verifyerror

我现有的Twitter集成代码正在运行,但现在却抛出了这个错误: -

11-11 02:30:34.835: E/dalvikvm(2278): Could not find class 'twitter4j.TwitterFactory', referenced from method com.myapp.TwitterApp.<init>

然后强制关闭此错误: -

11-11 02:30:35.265: E/AndroidRuntime(2278): FATAL EXCEPTION: main
11-11 02:30:35.265: E/AndroidRuntime(2278): java.lang.VerifyError: com/myapp/TwitterApp

这是在这句话上抛出错误: -

mTwitter = new TwitterApp(this, Share.twitter_consumer_key,
            Share.twitter_secret_key);

我的TwitterApp类代码是: -

package com.myapp;  

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.view.Window;



public class TwitterApp {
private Twitter mTwitter;
private TwitterSession mSession;
private AccessToken mAccessToken;
private CommonsHttpOAuthConsumer mHttpOauthConsumer;
private OAuthProvider mHttpOauthprovider;
private String mConsumerKey;
private String mSecretKey;
private ProgressDialog mProgressDlg;
private TwDialogListener mListener;
private Activity context;


public static final String  OAUTH_CALLBACK_SCHEME   = "x-oauthflow-twitter";
public static final String  OAUTH_CALLBACK_HOST     = "callback";
public static final String  CALLBACK_URL      = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
private static final String TWITTER_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize";
private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token";
public static final String MESSAGE = "Hello Everyone....";

public TwitterApp(Activity context, String consumerKey, String secretKey) {
    this.context = context;

    mTwitter = new TwitterFactory().getInstance();
    mSession = new TwitterSession(context);
    mProgressDlg = new ProgressDialog(context);

    mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);

    mConsumerKey = consumerKey;
    mSecretKey = secretKey;

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey,
            mSecretKey);

    String request_url=TWITTER_REQUEST_URL;
    String access_token_url=TWITTER_ACCESS_TOKEN_URL;
    String authorize_url=TWITTER_AUTHORZE_URL;

    mHttpOauthprovider = new DefaultOAuthProvider(
            request_url,
            access_token_url,
            authorize_url);
    mAccessToken = mSession.getAccessToken();

    configureToken();
}

public void setListener(TwDialogListener listener) {
    mListener = listener;
}

@SuppressWarnings("deprecation")
private void configureToken() {
    if (mAccessToken != null) {
        mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey);
        mTwitter.setOAuthAccessToken(mAccessToken);
    }
}

public boolean hasAccessToken() {
    return (mAccessToken == null) ? false : true;
}

public void resetAccessToken() {
    if (mAccessToken != null) {
        mSession.resetAccessToken();

        mAccessToken = null;
    }
}

public String getUsername() {
    return mSession.getUsername();
}

public void updateStatus(String status) throws Exception {
    try {
        mTwitter.updateStatus(status);
    } catch (TwitterException e) {
        throw e;
    }
}

public void authorize() {
    mProgressDlg.setMessage("Initializing ...");
    mProgressDlg.show();

    new Thread() {
        @Override
        public void run() {
            String authUrl = "";
            int what = 1;

            try {
                authUrl = mHttpOauthprovider.retrieveRequestToken(
                        mHttpOauthConsumer, CALLBACK_URL);
                what = 0;
            } catch (Exception e) {
                e.printStackTrace();
            }
            mHandler.sendMessage(mHandler
                    .obtainMessage(what, 1, 0, authUrl));
        }
    }.start();
}

public void processToken(String callbackUrl) {
    mProgressDlg.setMessage("Finalizing ...");
    mProgressDlg.show();

    final String verifier = getVerifier(callbackUrl);

    new Thread() {
        @Override
        public void run() {
            int what = 1;

            try {
                mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer,
                        verifier);

                mAccessToken = new AccessToken(
                        mHttpOauthConsumer.getToken(),
                        mHttpOauthConsumer.getTokenSecret());

                configureToken();

                User user = mTwitter.verifyCredentials();

                mSession.storeAccessToken(mAccessToken, user.getName());

                what = 0;
            } catch (Exception e) {
                e.printStackTrace();
            }

            mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
        }
    }.start();
}

private String getVerifier(String callbackUrl) {
    String verifier = "";

    try {
        callbackUrl = callbackUrl.replace("twitterapp", "http");

        URL url = new URL(callbackUrl);
        String query = url.getQuery();

        String array[] = query.split("&");

        for (String parameter : array) {
            String v[] = parameter.split("=");

            if (URLDecoder.decode(v[0]).equals(
                    oauth.signpost.OAuth.OAUTH_VERIFIER)) {
                verifier = URLDecoder.decode(v[1]);
                break;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return verifier;
}

private void showLoginDialog(String url) {
    final TwDialogListener listener = new TwDialogListener() {

        public void onComplete(String value) {
            processToken(value);
        }

        public void onError(String value) {
            mListener.onError("Failed opening authorization page");
        }
    };

    new TwitterDialog(context, url, listener).show();
}

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        mProgressDlg.dismiss();

        if (msg.what == 1) {
            if (msg.arg1 == 1)
                mListener.onError("Error getting request token");
            else
                mListener.onError("Error getting access token");
        } else {
            if (msg.arg1 == 1)
                showLoginDialog((String) msg.obj);
            else
                mListener.onComplete("");
        }
    }
};

public interface TwDialogListener {
    public void onComplete(String value);

    public void onError(String value);
}
}

package com.myapp; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import oauth.signpost.OAuthProvider; import oauth.signpost.basic.DefaultOAuthProvider; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.User; import twitter4j.auth.AccessToken; import android.app.Activity; import android.app.ProgressDialog; import android.os.Handler; import android.os.Message; import android.view.Window; public class TwitterApp { private Twitter mTwitter; private TwitterSession mSession; private AccessToken mAccessToken; private CommonsHttpOAuthConsumer mHttpOauthConsumer; private OAuthProvider mHttpOauthprovider; private String mConsumerKey; private String mSecretKey; private ProgressDialog mProgressDlg; private TwDialogListener mListener; private Activity context; public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter"; public static final String OAUTH_CALLBACK_HOST = "callback"; public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST; private static final String TWITTER_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"; private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize"; private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token"; public static final String MESSAGE = "Hello Everyone...."; public TwitterApp(Activity context, String consumerKey, String secretKey) { this.context = context; mTwitter = new TwitterFactory().getInstance(); mSession = new TwitterSession(context); mProgressDlg = new ProgressDialog(context); mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE); mConsumerKey = consumerKey; mSecretKey = secretKey; mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey); String request_url=TWITTER_REQUEST_URL; String access_token_url=TWITTER_ACCESS_TOKEN_URL; String authorize_url=TWITTER_AUTHORZE_URL; mHttpOauthprovider = new DefaultOAuthProvider( request_url, access_token_url, authorize_url); mAccessToken = mSession.getAccessToken(); configureToken(); } public void setListener(TwDialogListener listener) { mListener = listener; } @SuppressWarnings("deprecation") private void configureToken() { if (mAccessToken != null) { mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey); mTwitter.setOAuthAccessToken(mAccessToken); } } public boolean hasAccessToken() { return (mAccessToken == null) ? false : true; } public void resetAccessToken() { if (mAccessToken != null) { mSession.resetAccessToken(); mAccessToken = null; } } public String getUsername() { return mSession.getUsername(); } public void updateStatus(String status) throws Exception { try { mTwitter.updateStatus(status); } catch (TwitterException e) { throw e; } } public void authorize() { mProgressDlg.setMessage("Initializing ..."); mProgressDlg.show(); new Thread() { @Override public void run() { String authUrl = ""; int what = 1; try { authUrl = mHttpOauthprovider.retrieveRequestToken( mHttpOauthConsumer, CALLBACK_URL); what = 0; } catch (Exception e) { e.printStackTrace(); } mHandler.sendMessage(mHandler .obtainMessage(what, 1, 0, authUrl)); } }.start(); } public void processToken(String callbackUrl) { mProgressDlg.setMessage("Finalizing ..."); mProgressDlg.show(); final String verifier = getVerifier(callbackUrl); new Thread() { @Override public void run() { int what = 1; try { mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier); mAccessToken = new AccessToken( mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret()); configureToken(); User user = mTwitter.verifyCredentials(); mSession.storeAccessToken(mAccessToken, user.getName()); what = 0; } catch (Exception e) { e.printStackTrace(); } mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0)); } }.start(); } private String getVerifier(String callbackUrl) { String verifier = ""; try { callbackUrl = callbackUrl.replace("twitterapp", "http"); URL url = new URL(callbackUrl); String query = url.getQuery(); String array[] = query.split("&"); for (String parameter : array) { String v[] = parameter.split("="); if (URLDecoder.decode(v[0]).equals( oauth.signpost.OAuth.OAUTH_VERIFIER)) { verifier = URLDecoder.decode(v[1]); break; } } } catch (MalformedURLException e) { e.printStackTrace(); } return verifier; } private void showLoginDialog(String url) { final TwDialogListener listener = new TwDialogListener() { public void onComplete(String value) { processToken(value); } public void onError(String value) { mListener.onError("Failed opening authorization page"); } }; new TwitterDialog(context, url, listener).show(); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { mProgressDlg.dismiss(); if (msg.what == 1) { if (msg.arg1 == 1) mListener.onError("Error getting request token"); else mListener.onError("Error getting access token"); } else { if (msg.arg1 == 1) showLoginDialog((String) msg.obj); else mListener.onComplete(""); } } }; public interface TwDialogListener { public void onComplete(String value); public void onError(String value); } }

我已经实现了在此stackoverflow问题上接受的相同代码: why TwitterApp give VerifyError?

即时通讯使用最新的twitter4j-core-3.0.5.jar和项目我的构建目标是API 8。

包括的罐子是: -

请帮我弄清楚问题。感谢

0 个答案:

没有答案