Reputation: 540
[EDIT] I am trying to search some tweets from twitter using twitter4j version 4.0.4 as library. This is my method
public void processAll(View view){
Log.d("click","OKE");
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxx")
.setOAuthConsumerSecret("xxxx")
.setOAuthAccessToken("7xxxxL")
.setOAuthAccessTokenSecret("xxxxx");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Log.d("connected","OKE");
try {
Query query = new Query("handsome");
Log.d("TEST","OKE");
QueryResult result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
Log.d("RESULT","@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
}
}
catch (TwitterException te) {
te.printStackTrace();
Log.d("RESULT","Failed to search tweets: " + te.getMessage());
return;
}
}
But my app stopped when this method is called.
at com.fajarainul.coconut_dev.titikota.SetTimeActivity.processAll(SetTimeActivity.java:68)
references to
QueryResult result = twitter.search(query);
here my full logcat
09-01 21:17:52.862 12562-12562/com.fajarainul.coconut_dev.titikota E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
09-01 21:17:53.947 12562-12562/com.fajarainul.coconut_dev.titikota E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fajarainul.coconut_dev.titikota, PID: 12562
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3834)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18523)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3829)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18523)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251)
at java.net.InetAddress.getAllByName(InetAddress.java:229)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
at twitter4j.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:142)
at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1556)
at twitter4j.TwitterImpl.search(TwitterImpl.java:247)
at com.fajarainul.coconut_dev.titikota.SetTimeActivity.processAll(SetTimeActivity.java:70)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3829)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18523)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Thanks in advance!
Upvotes: 1
Views: 316
Reputation: 5149
You are throwing a NetworkOnMainThreadException
which means you are attempting to run network code (i.e. running a search via the twitter API) on the applications main thread.
You will need to move this code into a background thread using something like an AsyncTask
(see docs) for it to work.
I would suggest reading the docs and also the responses to the previous question on SO: How to fix android.os.NetworkOnMainThreadException?
Upvotes: 2