2

I am trying to integrate my application with Twitter. But I am getting some errors. I have used the code from this site. I have created an API for my app. I have set the website's URL to http://www.androidhive.info and the callback URL to http://androidhive.info. I updated my consumer and consumer key secret.

I am not sure why I am getting this error. I have attached the error logcat here:

03-18 21:36:13.971: E/AndroidRuntime(1339): FATAL EXCEPTION: main
03-18 21:36:13.971: E/AndroidRuntime(1339): android.os.NetworkOnMainThreadException
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at com.androidhive.twitterconnect.MainActivity.loginToTwitter(MainActivity.java:236)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at com.androidhive.twitterconnect.MainActivity.access$1(MainActivity.java:223)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at com.androidhive.twitterconnect.MainActivity$1.onClick(MainActivity.java:123)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.view.View.performClick(View.java:3480)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.view.View$PerformClick.run(View.java:13983)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.os.Handler.handleCallback(Handler.java:605)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.os.Looper.loop(Looper.java:137)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invoke(Method.java:511)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-18 21:36:13.971: E/AndroidRuntime(1339):     at dalvik.system.NativeStart.main(Native Method)
dda
  • 6,030
  • 2
  • 25
  • 34
Developer
  • 385
  • 1
  • 3
  • 18

3 Answers3

3

android.os.NetworkOnMainThreadException

From Android 3.0 you can't attempt to perform networking operation from Main(UI) Thread. So you have to move your code into background Thread and best solution is an usage of AsyncTask to reach your goal.

It's designated for tasks as yours. Also generally is a bad practise to make some long calculations, networking and other stuff on UI Thread. You should let your Activity classes clean and separate logic of application from an appearance.

Main advantages of AsyncTask: it's generic type(type safe), offers a few methods if you want to announce an user that "something is changed" and implementation isn't difficult to understand.

If you want some inspiration look at Zwitscher's github.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
0

It is bad practice to perform network operations on the main thread, which is why you are seeing this. It is prevented by the policy. If you really must do it for testing, put the following in your OnCreate:

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

Please remember that is is very bad practice to do this, and should ideally move your network code to an AsyncTask or a Thread.

Nova Entropy
  • 5,727
  • 1
  • 19
  • 32
0

You should move your code which attempts to use Twitter API (or perform any other network operation) either to a new AsyncTask or to a new Thread.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
Damian Jeżewski
  • 1,246
  • 2
  • 14
  • 21