- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 twitter 集成到我的 android 应用程序中,在打开启动应用程序之前发生异常 stackTrace 如下:-
07-26 16:01:34.004: E/AndroidRuntime(1043): FATAL EXCEPTION: main
07-26 16:01:34.004: E/AndroidRuntime(1043): java.lang.VerifyError: com.appstudioz.twitter.TwitterApp
07-26 16:01:34.004: E/AndroidRuntime(1043): at com.appstudioz.bidnear.NewsFeed.onCreate(NewsFeed.java:44)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.os.Looper.loop(Looper.java:132)
07-26 16:01:34.004: E/AndroidRuntime(1043): at android.app.ActivityThread.main(ActivityThread.java:4025)
07-26 16:01:34.004: E/AndroidRuntime(1043): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:01:34.004: E/AndroidRuntime(1043): at java.lang.reflect.Method.invoke(Method.java:491)
07-26 16:01:34.004: E/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-26 16:01:34.004: E/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-26 16:01:34.004: E/AndroidRuntime(1043): at dalvik.system.NativeStart.main(Native Method)
我正在使用这段代码:-
按钮mbutton;
private TwitterApp mTwitter;
ProgressDialog mProgressDialog = null;
private String url;
/** Called when the activity is first created. */
public static final String CONSUMER_KEY = "mdgzcl73NRkKaRzqVutQpA";
public static final String CONSUMER_SECRET="FVZwc6m4FZ5TbAU2uAfWhbxUMMcZ835Hp3VWBEF10";
private enum FROM {
TWITTER_POST, TWITTER_LOGIN
};
private enum MESSAGE {
SUCCESS, DUPLICATE, FAILED, CANCELLED
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);//newsfeed);
mbutton = (Button)findViewById(R.id.demo);
mbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
calltwitter();
}
});
mTwitter = new TwitterApp(NewsFeed.this, CONSUMER_KEY, CONSUMER_SECRET);
mTwitter.setListener(mTwLoginDialogListener);
//setTabs();
}
private void calltwitter()
{
if (mTwitter.hasAccessToken())
{
try
{
String comment = "I found this App Send SuccessFully..?";
String demo = "Hi ! Share Have been done ...";
try {
mTwitter.updateStatus(demo);
//mTwitter.updateStatus("rentfaster: "+demo+");
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e)
{
}
}
else
{
mTwitter.authorize();
}
}
/**
* Twitter Dialog Listner.
*/
//private TwDialogListener mTwLoginDialogListener = new TwDialogListener()
private TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
public void onError(String value) {
mTwitter.resetAccessToken();
}
public void onComplete(String value) {
String comment = "I found working fine";
try
{
String demo = "Hi ! Share Have been done ...";
try {
mTwitter.updateStatus(demo);
//mTwitter.updateStatus("rentfaster: "+demo+");
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e)
{
}
mTwitter.resetAccessToken();
}
};
private void setTabs()
{
}
在启动应用程序之前发生上述异常。我的要求是在 Twitter 上分享一些数据,但在第一步中我遇到了这个问题,请有人建议我!
最佳答案
为此使用 Twitter4j 库并使用以下代码将 Twitter 集成到您的应用程序中。
主 Activity .java
private TwitterApp mTwitter;
private static final String CONSUMER_KEY = "your consumer key";
private static final String CONSUMER_SECRET = "your consumer secret key";
private enum FROM {
TWITTER_POST, TWITTER_LOGIN
};
private enum MESSAGE {
SUCCESS, DUPLICATE, FAILED, CANCELLED
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTwitter = new TwitterApp(this, CONSUMER_KEY, CONSUMER_SECRET);
}
public void onClick(View v) {
mTwitter.setListener(mTwLoginDialogListener);
mTwitter.resetAccessToken();
if (mTwitter.hasAccessToken() == true) {
try {
mTwitter.updateStatus(TwitterApp.MESSAGE);
postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
} catch (Exception e) {
if (e.getMessage().toString().contains("duplicate")) {
postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
}
e.printStackTrace();
}
mTwitter.resetAccessToken();
} else {
mTwitter.authorize();
}
}
private void postAsToast(FROM twitterPost, MESSAGE success) {
switch (twitterPost) {
case TWITTER_LOGIN:
switch (success) {
case SUCCESS:
Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG)
.show();
break;
case FAILED:
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
default:
break;
}
break;
case TWITTER_POST:
switch (success) {
case SUCCESS:
Toast.makeText(this, "Posted Successfully", Toast.LENGTH_LONG)
.show();
break;
case FAILED:
Toast.makeText(this, "Posting Failed", Toast.LENGTH_LONG)
.show();
break;
case DUPLICATE:
Toast.makeText(this,
"Posting Failed because of duplicate message...",
Toast.LENGTH_LONG).show();
default:
break;
}
break;
}
}
private TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
@Override
public void onError(String value) {
postAsToast(FROM.TWITTER_LOGIN, MESSAGE.FAILED);
Log.e("TWITTER", value);
mTwitter.resetAccessToken();
}
@Override
public void onComplete(String value) {
try {
mTwitter.updateStatus(TwitterApp.MESSAGE);
postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
} catch (Exception e) {
if (e.getMessage().toString().contains("duplicate")) {
postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
}
e.printStackTrace();
}
mTwitter.resetAccessToken();
}
};
将以下 3 个类添加到新包“com.twitter.android”
第 1 类:-(TwitterApp.java)
package com.twitter.android;
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.http.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);
}
}
第 2 类:- (TwitterDialog.java)
package com.twitter.android;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.twitter.android.TwitterApp.TwDialogListener;
public class TwitterDialog extends Dialog {
static final float[] DIMENSIONS_LANDSCAPE = { 460, 260 };
static final float[] DIMENSIONS_PORTRAIT = { 280, 420 };
static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
static final int MARGIN = 4;
static final int PADDING = 2;
private String mUrl;
private TwDialogListener mListener;
private ProgressDialog mSpinner;
private WebView mWebView;
private LinearLayout mContent;
private TextView mTitle;
private boolean progressDialogRunning = false;
public TwitterDialog(Context context, String url, TwDialogListener listener) {
super(context);
mUrl = url;
mListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSpinner = new ProgressDialog(getContext());
mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
mSpinner.setMessage("Loading...");
mContent = new LinearLayout(getContext());
mContent.setOrientation(LinearLayout.VERTICAL);
setUpTitle();
setUpWebView();
Display display = getWindow().getWindowManager().getDefaultDisplay();
final float scale = getContext().getResources().getDisplayMetrics().density;
float[] dimensions = (display.getWidth() < display.getHeight()) ? DIMENSIONS_PORTRAIT
: DIMENSIONS_LANDSCAPE;
addContentView(mContent, new FrameLayout.LayoutParams(
(int) (dimensions[0] * scale + 0.5f), (int) (dimensions[1]
* scale + 0.5f)));
}
private void setUpTitle() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
Drawable icon = getContext().getResources().getDrawable(
R.drawable.twitter_icon);
mTitle = new TextView(getContext());
mTitle.setText("Twitter");
mTitle.setTextColor(Color.WHITE);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
mTitle.setBackgroundColor(0xFFbbd7e9);
mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
mContent.addView(mTitle);
}
private void setUpWebView() {
mWebView = new WebView(getContext());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new TwitterWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
mWebView.setLayoutParams(FILL);
mContent.addView(mWebView);
}
private class TwitterWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(TwitterApp.CALLBACK_URL)) {
mListener.onComplete(url);
TwitterDialog.this.dismiss();
return true;
} else if (url.startsWith("authorize")) {
return false;
}
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
mListener.onError(description);
TwitterDialog.this.dismiss();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mSpinner.show();
progressDialogRunning = true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String title = mWebView.getTitle();
if (title != null && title.length() > 0) {
mTitle.setText(title);
}
progressDialogRunning = false;
mSpinner.dismiss();
}
}
@Override
protected void onStop() {
progressDialogRunning = false;
super.onStop();
}
public void onBackPressed() {
if(!progressDialogRunning){
TwitterDialog.this.dismiss();
}
}
}
第 3 类:- (TwitterSession.java)
package com.twitter.android;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.Context;
import twitter4j.http.AccessToken;
public class TwitterSession {
private SharedPreferences sharedPref;
private Editor editor;
private static final String TWEET_AUTH_KEY = "auth_key";
private static final String TWEET_AUTH_SECRET_KEY = "auth_secret_key";
private static final String TWEET_USER_NAME = "user_name";
private static final String SHARED = "Twitter_Preferences";
public TwitterSession(Context context) {
sharedPref = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE);
editor = sharedPref.edit();
}
public void storeAccessToken(AccessToken accessToken, String username) {
editor.putString(TWEET_AUTH_KEY, accessToken.getToken());
editor.putString(TWEET_AUTH_SECRET_KEY, accessToken.getTokenSecret());
editor.putString(TWEET_USER_NAME, username);
editor.commit();
}
public void resetAccessToken() {
editor.putString(TWEET_AUTH_KEY, null);
editor.putString(TWEET_AUTH_SECRET_KEY, null);
editor.putString(TWEET_USER_NAME, null);
editor.commit();
}
public String getUsername() {
return sharedPref.getString(TWEET_USER_NAME, "");
}
public AccessToken getAccessToken() {
String token = sharedPref.getString(TWEET_AUTH_KEY, null);
String tokenSecret = sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);
if (token != null && tokenSecret != null)
return new AccessToken(token, tokenSecret);
else
return null;
}
}
并将以下 Jar 文件作为引用库添加到您的项目中:-
1) signpost-commonshttp4-1.2.1.1.jar
2) signpost-core-1.2.1.1.jar
3)路标-jetty6-1.2.1.1.jar
4) twitter4j-core-2.1.6.jar
关于android - 为什么 TwitterApp 给出 VerifyError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11667184/
我有一个Gradle 4.4.1脚本,偶尔会失败。这通常在运行test任务之前在多台计算机上发生。 这是相关的日志(在运行gradlew test --info时获取): Up-to-date che
我正在使用“打开模块设置 > 新建 > 导入 .JAR/.AAR 包”向我的项目(.aar)添加一个库。 它在应用程序打开时正确构建,但是当实例化该库的第一个类时,它给我提供了该类的“Java.lan
我在 J2EE 容器中偶尔收到以下错误。有时容器会启动,没有任何问题,有时容器不会启动,因为这个错误,以前有人见过这个错误吗...?可能是什么原因..?它是否涉及任何类加载器/安全问题..? java
我正在为 2.2 (minSdkVersion=8) 开发,突然出现这个错误: arbitrarily rejecting large method (regs=75 count=28584) rej
我在我的应用程序中使用指纹传感器。我知道 api 可用于 marshmallow 及以上操作系统。因此,在我的类里面运行时,我正在动态检查 sdk 版本。 即使指纹代码不执行,我在 4.0 andro
ERROR [org.springframework.web.context.ContextLoader:313] - Context initialization failed java.lang.
我正在使用Hadoop的Java项目中工作,并且有一个java.lang.VerifyError,我不知道如何解决它。我看到有人遇到相同类型的问题,但没有答案,或者解决方案在我的情况下不起作用。 我的
我正在扩展 JFreeChart ChartComposite 类。当我尝试创建扩展类的实例时,我收到 java.lang.VerifyError : .... 函数调用的不兼容对象参数 错误。 最佳
我使用 jcifs-1.3.17 库来检查共享文件夹上的一些文件。一周前,我发现我的代码无法正常工作。我遇到 500 服务器错误,如下所示: (类:jcifs/smb/NtlmPasswordAuth
有时我可以在崩溃报告服务日志中找到如下所示 Caused by: java.lang.VerifyError: com/google/android/gms/auth/GoogleAuthUtil
我正在开展一个项目,我们希望在该项目中重用近期类似项目的代码。然而,这一次,我们希望将代码拆分到三个库中,以便在未来的项目中更容易使用,我们也将在这些项目中重复使用这些代码。 我将像在 Netbean
我正在将我的appengine-项目从ant构建迁移到maven,我使用带有JDO 2的java 8和maven-datanucleus插件来进行类增强和maven appengine-plugin
提前道歉,但我以前从未见过这个错误,也不知道该包含什么。我正在使用 NetBeans 并突然开始出现此错误: Exception in thread "AWT-EventQueue-0" java.l
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我有一个应用程序,它使用 overridePendingTransition 在从一个 Activity 转换到另一个 Activity 时执行一些自定义动画。这在 Android 2.0 中可用,但
目前正在经历一场试图修复 java.lang.verifyerror 的噩梦,遇到了 eclipse 问题,所以我决定卸载并重新安装它,一切似乎都很正常,直到我尝试启动该应用程序,那时我注意到我无法启
我最近开始为我自己的语言编写一个编译器,但是当我调用 main 方法时它开始抛出异常。它适用于我的其他测试类,但它不想使用这个。据我所知,在此类和其他类之间调用方法的方式没有任何不同。这是异常(exc
我正在开发一个 Android 项目,该项目使用我正在创建的单独 JAR 文件中的类和方法,问题出在一个名为 XpathUtil 的特定实用程序类,该类抛出一个 VerifyError 每次我尝试调用
public class Config { public static Ref s = new Ref(new Config()); static class Ref {
在编写一些 Java 代码时,我遇到了一个我无法识别的异常,即 java.lang.VerifyError。一些谷歌搜索表明这通常是一个 jvm/javac 错误,我很好奇我的情况是否如此。 我怀疑的
我是一名优秀的程序员,十分优秀!