我正在使用库Twitter4J将apk与Twitter连接。我想在twitter上发布一些信息。我正在使用这种方式:
try {
AccessToken accessToken = new AccessToken(prefs.getTwitterToken(),prefs.getTwitterTokenSecret());
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(MyConstants.TWITTER_CONSUMER_KEY,
MyConstants.TWITTER_CONSUMER_SECRET ,accessToken);
String shareText= "Super";
Status status = twitter.updateStatus(shareText);
int statusId = (int)status.getId();
Toast.makeText(getActivity(), "Tweet Successfull", Toast.LENGTH_LONG).show();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
但我得到错误:
04-28 20:42:17.829: E/AndroidRuntime(1537): FATAL EXCEPTION: main
04-28 20:42:17.829: E/AndroidRuntime(1537): android.os.NetworkOnMainThreadException
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
04-28 20:42:17.829: E/AndroidRuntime(1537): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-28 20:42:17.829: E/AndroidRuntime(1537): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-28 20:42:17.829: E/AndroidRuntime(1537): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-28 20:42:17.829: E/AndroidRuntime(1537): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
04-28 20:42:17.829: E/AndroidRuntime(1537): at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:150)
04-28 20:42:17.829: E/AndroidRuntime(1537): at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
04-28 20:42:17.829: E/AndroidRuntime(1537): at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
04-28 20:42:17.829: E/AndroidRuntime(1537): at twitter4j.TwitterImpl.post(TwitterImpl.java:1871)
04-28 20:42:17.829: E/AndroidRuntime(1537): at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:241)
04-28 20:42:17.829: E/AndroidRuntime(1537): at pl.spot.tccsc.fragments.TeamsTeamOwnerFragment$1.onClick(TeamsTeamOwnerFragment.java:108)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.view.View.performClick(View.java:4211)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.view.View$PerformClick.run(View.java:17267)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.os.Handler.handleCallback(Handler.java:615)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.os.Looper.loop(Looper.java:137)
04-28 20:42:17.829: E/AndroidRuntime(1537): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-28 20:42:17.829: E/AndroidRuntime(1537): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 20:42:17.829: E/AndroidRuntime(1537): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 20:42:17.829: E/AndroidRuntime(1537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-28 20:42:17.829: E/AndroidRuntime(1537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-28 20:42:17.829: E/AndroidRuntime(1537): at dalvik.system.NativeStart.main(Native Method)
如何在Android上发布Twitter或解决该问题?
答案 0 :(得分:1)
04-28 20:42:17.829: E/AndroidRuntime(1537): android.os.NetworkOnMainThreadException
你得到NetworkOnMainThreadException
。使用AsyncTask
来避免这种情况。