分享推特时Twitter4j崩溃了

时间:2014-08-24 13:19:31

标签: android twitter twitter4j

我想在推特上发布推文,但点击推特按钮后应用程序崩溃了。 我的Twitter版本是twitter4j-core-3.0.5.jar。我是否必须更改清单中的设置?

我收到了illegalStateException。

public void onShareTwitter(View v) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey(CONSUMER_KEY)
      .setOAuthConsumerSecret(CONSUMER_SECRET)
      .setOAuthAccessToken(ACCESS_TOKEN)
      .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    Status status = null;
    try {
        status = twitter.updateStatus("Hello");
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
}

ERROR:

08-24 21:28:14.638: E/AndroidRuntime(11030): FATAL EXCEPTION: main
08-24 21:28:14.638: E/AndroidRuntime(11030): java.lang.IllegalStateException: Could not execute method of the activity
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.view.View$1.onClick(View.java:3724)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.view.View.performClick(View.java:4261)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.view.View$PerformClick.run(View.java:17356)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.os.Handler.handleCallback(Handler.java:615)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.os.Looper.loop(Looper.java:137)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.lang.reflect.Method.invoke(Method.java:511)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at dalvik.system.NativeStart.main(Native Method)
08-24 21:28:14.638: E/AndroidRuntime(11030): Caused by: java.lang.reflect.InvocationTargetException
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.lang.reflect.Method.invoke(Method.java:511)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.view.View$1.onClick(View.java:3719)
08-24 21:28:14.638: E/AndroidRuntime(11030):    ... 11 more
08-24 21:28:14.638: E/AndroidRuntime(11030): Caused by: android.os.NetworkOnMainThreadException
08-24 21:28:14.638: E/AndroidRuntime(11030):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:135)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at twitter4j.TwitterImpl.post(TwitterImpl.java:1965)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:264)
08-24 21:28:14.638: E/AndroidRuntime(11030):    at com.dante.sphar.PhotoPreviewActivity.onShareTwitter(PhotoPreviewActivity.java:228)
08-24 21:28:14.638: E/AndroidRuntime(11030):    ... 14 more  

更改asynctask后更新错误

08-24 21:48:56.223: W/System.err(12740): No authentication challenges found

08-24 21:48:56.223:W / System.err(12740):相关讨论可在互联网上找到: 08-24 21:48:56.223:W / System.err(12740):http://www.google.co.jp/search?q=b2b52c28或 08-24 21:48:56.223:W / System.err(12740):http://www.google.co.jp/search?q=1206f231 08-24 21:48:56.223:W / System.err(12740):TwitterException {exceptionCode = [b2b52c28-1206f231 4ef6906d-2ed74fb5],statusCode = -1,message = null,code = -1,retryAfter = -1, rateLimitStatus = null,version = 3.0.5} 08-24 21:48:56.228:W / System.err(12740):at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177) 08-24 21:48:56.228:W / System.err(12740):at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61) 08-24 21:48:56.228:W / System.err(12740):at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98) 08-24 21:48:56.228:W / System.err(12740):在twitter4j.TwitterImpl.post(TwitterImpl.java:1965) 08-24 21:48:56.228:W / System.err(12740):在twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:264) 08-24 21:48:56.228:W / System.err(12740):at com.dante.sphar.PhotoPreviewActivity.postToTwitter(PhotoPreviewActivity.java:247) 08-24 21:48:56.228:W / System.err(12740):at com.dante.sphar.PhotoPreviewActivity.access $ 1(PhotoPreviewActivity.java:236) 08-24 21:48:56.228:W / System.err(12740):at com.dante.sphar.PhotoPreviewActivity $ 1.run(PhotoPreviewActivity.java:224) 08-24 21:48:56.228:W / System.err(12740):at java.lang.Thread.run(Thread.java:856) 08-24 21:48:56.228:W / System.err(12740):引起:java.io.IOException:未发现身份验证问题 08-24 21:48:56.228:W / System.err(12740):at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427) 08-24 21:48:56.233:W / System.err(12740):at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407) 08-24 21:48:56.233:W / System.err(12740):at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356) 08-24 21:48:56.233:W / System.err(12740):at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292) 08-24 21:48:56.233:W / System.err(12740):at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486) 08-24 21:48:56.233:W / System.err(12740):at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134) 08-24 21:48:56.233:W / System.err(12740):at twitter4j.internal.http.HttpResponseImpl。(HttpResponseImpl.java:34) 08-24 21:48:56.233:W / System.err(12740):at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)

2 个答案:

答案 0 :(得分:2)

<强> NetworkOnMainThreadException

  

当应用程序尝试在其主线程上执行网络操作时引发的异常。

在线程或asynctask中调用 onShareTwitter ,如下所示:

Thread thread = new Thread(new Runnable(){
@Override
public void run() {
    try {
        //Your code goes here
        onShareTwitter(view);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
});

thread.start(); 

答案 1 :(得分:0)

将状态调用函数放入异步任务中,尝试以下操作。如下所示:

public ProgressDialog pdProgressCircleSend;

    public class UseTweeter extends AsyncTask<String,String,String>{
    String msg;

    @Override
    protected void onPreExecute() {
        pdProgressCircleSend=new ProgressDialog(getActivity());
        pdProgressCircleSend.setMessage(Constants.POSTING_TWEET);
        pdProgressCircleSend.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        onShareTwitter();
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        if(pdProgressCircleSend!=null && pdProgressCircleSend.isShowing()) {
            pdProgressCircleSend.dismiss();
        }
        Toast.makeText(getContext(), "Tweeted Successfully", Toast.LENGTH_SHORT).show();
    }
}
public void onShareTwitter(View v) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
  .setOAuthConsumerKey(CONSUMER_KEY)
  .setOAuthConsumerSecret(CONSUMER_SECRET)
  .setOAuthAccessToken(ACCESS_TOKEN)
  .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Status status = null;
try {
    status = twitter.updateStatus("Hello");
} catch (TwitterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}