但使书种多,会有岁稔时。这篇文章主要讲述使用twitter4j库的Twitter Android登录错误相关的知识,希望能为你提供帮助。
我正在尝试使用Twitter4j库登录我的Twitter应用程序。但每当我点击我的登录按钮时,我的应用程序就会崩溃。
这是我班上的方法 -
btnLoginTwitter.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View arg0) {
// Call login twitter function
loginToTwitter();
}
});
/**
* Function to login twitter
* */
private void loginToTwitter() {
// Check if already logged in
if (!isTwitterLoggedInAlready()) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException e) {
e.printStackTrace();
}
} else {
// user already logged into twitter
Toast.makeText(getApplicationContext(),
"Already Logged into twitter", Toast.LENGTH_LONG).show();
}
}
当我按下登录按钮时,它显示以下LogCat-
07-18 00:55:00.726: E/androidRuntime(1185): FATAL EXCEPTION: main
07-18 00:55:00.726: E/AndroidRuntime(1185): android.os.NetworkOnMainThreadException
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
07-18 00:55:00.726: E/AndroidRuntime(1185):at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-18 00:55:00.726: E/AndroidRuntime(1185):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-18 00:55:00.726: E/AndroidRuntime(1185):at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpConnection.<
init>
(HttpConnection.java:70)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpConnection.<
init>
(HttpConnection.java:50)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
07-18 00:55:00.726: E/AndroidRuntime(1185):at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
07-18 00:55:00.726: E/AndroidRuntime(1185):at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
07-18 00:55:00.726: E/AndroidRuntime(1185):at com.androidhive.twitterconnect.MainActivity.loginToTwitter(MainActivity.java:236)
07-18 00:55:00.726: E/AndroidRuntime(1185):at com.androidhive.twitterconnect.MainActivity.access$1(MainActivity.java:223)
07-18 00:55:00.726: E/AndroidRuntime(1185):at com.androidhive.twitterconnect.MainActivity$1.onClick(MainActivity.java:123)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.view.View.performClick(View.java:4240)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.view.View$PerformClick.run(View.java:17721)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.os.Handler.handleCallback(Handler.java:730)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.os.Looper.loop(Looper.java:137)
07-18 00:55:00.726: E/AndroidRuntime(1185):at android.app.ActivityThread.main(ActivityThread.java:5103)
07-18 00:55:00.726: E/AndroidRuntime(1185):at java.lang.reflect.Method.invokeNative(Native Method)
07-18 00:55:00.726: E/AndroidRuntime(1185):at java.lang.reflect.Method.invoke(Method.java:525)
07-18 00:55:00.726: E/AndroidRuntime(1185):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-18 00:55:00.726: E/AndroidRuntime(1185):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 00:55:00.726: E/AndroidRuntime(1185):at dalvik.system.NativeStart.main(Native Method)
答案'android.os.NetworkOnMainThreadException'意味着你正在执行网络相关的功能,如Http post,get等等你的主GUI线程..所有你要做的就是把这个东西放在
AsyncTask
这样做:
private void loginToTwitter()
{
new AsyncTask<
Void, Void, Boolean>
() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}@Override
protected Boolean doInBackground(Void... params) {
if (!isTwitterLoggedInAlready()) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
return true;
} catch (TwitterException e) {
e.printStackTrace();
}
} else {
return false;
}
}@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result)
{
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(requestToken.getAuthenticationURL())));
}else{
Toast.makeText(getApplicationContext(),
"Already Logged into twitter", Toast.LENGTH_LONG).show();
}
}
}.execute();
}
注意:您无法从
doInBackgroung()
访问GUI线程,您可以从preExecute
和postExecute
方法访问GUI线程另一答案您正在应用程序的主线程中执行耗时的任务。这就是为什么它给
android.os.NetworkOnMainThreadException
错误。【使用twitter4j库的Twitter Android登录错误】尝试通过实现
doInBackground()
将代码放在AsyncTask
方法中,如下所示:private class WSCalling extends AsyncTask<
Void, Void, Integer>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}@Override
protectedInteger doInBackground(Void... params) {
if (!isTwitterLoggedInAlready()) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
} catch (TwitterException e) {
e.printStackTrace();
}
}
return requestToken;
}@Override
protected void onPostExecute(Integer args) {
if(args != null)
{
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(args.getAuthenticationURL())));
}else {
// user already logged into twitter
Toast.makeText(getApplicationContext(),
"Already Logged into twitter", Toast.LENGTH_LONG).show();
}
}
}
推荐阅读
- 当Apple从设置中删除时,如何在Xcode 9 Simulator中访问Twitter()
- Kotlin范围函数(Android EditText支持* apply *,* let *和* run *但不支持* with *)
- 使用contentResolver将docx转换为Base64会在Android上提供不可读的格式
- Koin如何注入Android活动/ appcompatactivity之外
- 在Mapbox Android中回调locationChanged
- 如何在kotlin android中将pdf文件编码为base64字符串
- 从Kotlin构造函数android中的Parcelable读取List列表
- 如何将加载覆盖进度条覆盖到android的“底部导航视图”(使用约束布局)
- 在浏览器中打开URL,从android上的shell脚本