{"id":1437,"date":"2022-08-30T15:16:31","date_gmt":"2022-08-30T15:16:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/twitter4j-code-does-not-work-on-ics-and-jellybean-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:31","modified_gmt":"2022-08-30T15:16:31","slug":"twitter4j-code-does-not-work-on-ics-and-jellybean-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/twitter4j-code-does-not-work-on-ics-and-jellybean-collection-of-common-programming-errors\/","title":{"rendered":"twitter4j code does not work on ICS and JellyBean-Collection of common programming errors"},"content":{"rendered":"<p>I am using twitter4J to post tweet on twitter<\/p>\n<p>Here i Change the Code according to your suggestion . i do some google search. The problem is When i try to shift from main activity to twitter activity it show force close. Main activity is = &#8220;MainActivity&#8221; twitter activity is = &#8220;twiti_backup&#8221; I think there is problem in Manifestfile but i dont know what was it.<\/p>\n<pre><code>public class twiti_backup extends Activity {\n\nprivate static final String TAG = \"Blundell.TweetToTwitterActivity\";\n\n\nprivate static final String PREF_ACCESS_TOKEN = \"\";\n\nprivate static final String PREF_ACCESS_TOKEN_SECRET = \"\";\n\nprivate static final String CONSUMER_KEY = \"\";\n\nprivate static final String CONSUMER_SECRET = \"\"; \nprivate static final String CALLBACK_URL = \"android:\/\/\/\";\n\nprivate SharedPreferences mPrefs;\n\nprivate Twitter mTwitter;\n\nprivate RequestToken mReqToken;\n\nprivate Button mLoginButton;\nprivate Button mTweetButton;\n\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    Log.i(TAG, \"Loading TweetToTwitterActivity\");\n    setContentView(R.layout.twite);\n\n\n    mPrefs = getSharedPreferences(\"twitterPrefs\", MODE_PRIVATE);\n\n    mTwitter = new TwitterFactory().getInstance();\n\n    mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);\n\n\n    mLoginButton = (Button) findViewById(R.id.login_button);\n    mTweetButton = (Button) findViewById(R.id.tweet_button);\n}\n\n\npublic void buttonLogin(View v) {\n    Log.i(TAG, \"Login Pressed\");\n    if (mPrefs.contains(PREF_ACCESS_TOKEN)) {\n        Log.i(TAG, \"Repeat User\");\n        loginAuthorisedUser();\n    } else {\n        Log.i(TAG, \"New User\");\n        loginNewUser();\n    }\n}\n\n\npublic void buttonTweet(View v) {\n    Log.i(TAG, \"Tweet Pressed\");\n    tweetMessage();\n}\n\n\nprivate void loginNewUser() {\n    try {\n        Log.i(TAG, \"Request App Authentication\");\n        mReqToken = mTwitter.getOAuthRequestToken(CALLBACK_URL);\n\n        Log.i(TAG, \"Starting Webview to login to twitter\");\n        WebView twitterSite = new WebView(this);\n        twitterSite.loadUrl(mReqToken.getAuthenticationURL());\n        setContentView(twitterSite);\n\n    } catch (TwitterException e) {\n        Toast.makeText(this, \"Twitter Login error, try again later\", Toast.LENGTH_SHORT).show();\n    }\n}\n\n\nprivate void loginAuthorisedUser() {\n    String token = mPrefs.getString(PREF_ACCESS_TOKEN, null);\n    String secret = mPrefs.getString(PREF_ACCESS_TOKEN_SECRET, null);\n\n    \/\/ Create the twitter access token from the credentials we got previously\n    AccessToken at = new AccessToken(token, secret);\n\n    mTwitter.setOAuthAccessToken(at);\n\n    Toast.makeText(this, \"Welcome back\", Toast.LENGTH_SHORT).show();\n\n    enableTweetButton();\n}\n\n@Override\nprotected void onNewIntent(Intent intent) {\n    super.onNewIntent(intent);\n    Log.i(TAG, \"New Intent Arrived\");\n    dealWithTwitterResponse(intent);\n}\n\n@Override\nprotected void onResume() {\n    super.onResume();\n    Log.i(TAG, \"Arrived at onResume\");\n}\n\n\nprivate void dealWithTwitterResponse(Intent intent) {\n    Uri uri = intent.getData();\n    if (uri != null &amp;&amp; uri.toString().startsWith(CALLBACK_URL)) { \/\/ If the user has just logged in\n        String oauthVerifier = uri.getQueryParameter(\"oauth_verifier\");\n\n        authoriseNewUser(oauthVerifier);\n    }\n}\n\n\nprivate void authoriseNewUser(String oauthVerifier) {\n    try {\n        AccessToken at = mTwitter.getOAuthAccessToken(mReqToken, oauthVerifier);\n        mTwitter.setOAuthAccessToken(at);\n\n        saveAccessToken(at);\n\n        \/\/ Set the content view back after we changed to a webview\n        setContentView(R.layout.twite);\n\n        enableTweetButton();\n    } catch (TwitterException e) {\n        Toast.makeText(this, \"Twitter auth error x01, try again later\", Toast.LENGTH_SHORT).show();\n    }\n}\n\n\nprivate void enableTweetButton() {\n    Log.i(TAG, \"User logged in - allowing to tweet\");\n    mLoginButton.setEnabled(false);\n    mTweetButton.setEnabled(true);\n}\n\n\nprivate void tweetMessage() {\n    try {\n        mTwitter.updateStatus(\"Test - Tweeting with @Blundell_apps #AndroidDev Tutorial using #Twitter4j http:\/\/blog.blundell-apps.com\/sending-a-tweet\/\");\n\n        Toast.makeText(this, \"Tweet Successful!\", Toast.LENGTH_SHORT).show();\n    } catch (TwitterException e) {\n        Toast.makeText(this, \"Tweet error, try again later\", Toast.LENGTH_SHORT).show();\n    }\n}\n\nprivate void saveAccessToken(AccessToken at) {\n    String token = at.getToken();\n    String secret = at.getTokenSecret();\n    Editor editor = mPrefs.edit();\n    editor.putString(PREF_ACCESS_TOKEN, token);\n    editor.putString(PREF_ACCESS_TOKEN_SECRET, secret);\n    editor.commit();\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>And here is Manifest<\/p>\n<pre><code>  \n    \n        \n            \n            \n        \n    \n         \n            \n            \n                                         \n  \n    \n     \n\n<\/code><\/pre>\n<p>Here is Log cat when i try to launch twiti_backup from main activity<\/p>\n<pre><code>W\/dalvikvm(16357): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) \nE\/AndroidRuntime(16357): FATAL EXCEPTION: main E\/AndroidRuntime(16357): java.lang.VerifyError: com.example.uitest.twiti_backup\nE\/AndroidRuntime(16357): at java.lang.Class.newInstanceImpl(Native Method)\nE\/AndroidRuntime(16357): at java.lang.Class.newInstance(Class.java:1409)\nE\/AndroidRuntime(16357): at android.app.Instrumentation.newActivity(Instrumentation.java:1040)\nE\/AndroidRuntime(16357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1735)\nE\/AndroidRuntime(16357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)\nE\/AndroidRuntime(16357): at android.app.ActivityThread.access$1500(ActivityThread.java:132)\nE\/AndroidRuntime(16357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)\nE\/AndroidRuntime(16357): at android.os.Handler.dispatchMessage(Handler.java:99)\nE\/AndroidRuntime(16357): at android.os.Looper.loop(Looper.java:143)\nE\/AndroidRuntime(16357): at android.app.ActivityThread.main(ActivityThread.java:4263)\nE\/AndroidRuntime(16357): at java.lang.reflect.Method.invokeNative(Native Method)\nE\/AndroidRuntime(16357): at java.lang.reflect.Method.invoke(Method.java:507)\nE\/AndroidRuntime(16357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)\nE\/AndroidRuntime(16357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)\nE\/AndroidRuntime(16357): at dalvik.system.NativeStart.main(Native Method)\n<\/code><\/pre>\n<ol>\n<li>\n<p>Add below two line code into your main activity&#8217;s onCreate method after setcontentview() line, it will solve your problem.<\/p>\n<pre><code>if (android.os.Build.VERSION.SDK_INT &gt; 9) {\n    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();\n    StrictMode.setThreadPolicy(policy);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p><b>First<\/b>, your <code>CALLBACKURL = \"T4J_OAuth:\/\/callback_main\"<\/code> is wrong when compare with your <code>manifest<\/code>, it should be:<code>CALLBACKURL = \"T4J_OAuth:\/\/\/\"<\/code><\/p>\n<p><b>Second<\/b>: <code>android:launchMode=\"singleTop\"<\/code> should be <code>android:launchMode=\"singleInstance\"<\/code><\/p>\n<p>Maybe there are some more bugs, but you should fix it yourseft!<\/p>\n<p><b>Edited:<\/b> You should declare <code>Provider<\/code> and <code>Consumer<\/code>:<\/p>\n<pre><code>consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRETE);\nprovider = new DefaultOAuthProvider(\n            \"http:\/\/twitter.com\/oauth\/request_token\",\n            \"http:\/\/twitter.com\/oauth\/access_token\",\n            \"http:\/\/twitter.com\/oauth\/authorize\");\nprovider.retrieveRequestToken(consumer, CALL_BACK);\nstartActivity(new Intent(Intent.ACTION_VIEW, Uri\n                    .parse(result)));\n<\/code><\/pre>\n<p>Also, you should create some <code>AsyncTask<\/code> to call those statments: <code>rovider.retrieveRequestToken(consumer, CALL_BACK);<\/code> and <code>provider.retrieveAccessToken(consumer, verifier);<\/code><\/p>\n<p><b>Edited<\/b> Look at this tutorial, just change the <code>CallBack_Url = \"...\"<\/code> to <code>CallBack_URL = \"callback:\/\/\/\"<\/code> and using below code to create <code>Twitter<\/code> object:<\/p>\n<pre><code>ConfigurationBuilder cb = new ConfigurationBuilder();\ncb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY)\n.setOAuthConsumerSecret(CONSUMER_SECRETE)\n\/\/ accessTokenKey and accessTokenSecrete are what you get after redirecting\n\/\/   back from your oauthentication\n.setOAuthAccessToken(accessTokenKey)\n.setOAuthAccessTokenSecret(accessTokenSecrete)\n.setMediaProviderAPIKey(TWITPIC_API_KEY);\nConfiguration config = cb.build();\nTwitterFactory tf = new TwitterFactory(config);\nTwitter twitter = tf.getInstance();\n<\/code><\/pre>\n<p><b>Notice:<\/b> Don&#8217;t let the <code>call back url<\/code> property in your <code>twitter app<\/code> null, put any dummy link into it! (App of your twitter account!! not your android app!) Sorry if there&#8217;s any type error!<\/p>\n<p><b>Edit<\/b> You got an <code>Verifier<\/code> error because of your verifier is wrong! I used <code>signpost<\/code> lib to get <code>verifier<\/code>: <code>String verifier = data .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);<\/code> I think you should try to use <code>signpost<\/code> lib!<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:53:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am using twitter4J to post tweet on twitter Here i Change the Code according to your suggestion . i do some google search. The problem is When i try to shift from main activity to twitter activity it show force close. Main activity is = &#8220;MainActivity&#8221; twitter activity is = &#8220;twiti_backup&#8221; I think there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1437","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}