1

我无法使用 jtwitter jar 发布到 Twitter。我收到以下错误:

10-07 15:13:39.975: E/AndroidRuntime(2316): FATAL EXCEPTION: main
10-07 15:13:39.975: E/AndroidRuntime(2316): winterwell.jtwitter.TwitterException: android.os.NetworkOnMainThreadException
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.URLConnectionHttpClient.getPage2_ex(URLConnectionHttpClient.java:297)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.URLConnectionHttpClient.post(URLConnectionHttpClient.java:370)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:2762)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:2694)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.Twitter.setStatus(Twitter.java:2482)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at com.example.yamba.StatusActivity.onClick(StatusActivity.java:29)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.view.View.performClick(View.java:3511)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.view.View$PerformClick.run(View.java:14105)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.os.Handler.handleCallback(Handler.java:605)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.os.Looper.loop(Looper.java:137)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.app.ActivityThread.main(ActivityThread.java:4446)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at dalvik.system.NativeStart.main(Native Method)
10-07 15:13:39.975: E/AndroidRuntime(2316): Caused by: android.os.NetworkOnMainThreadException
10-07 15:13:39.975: E/AndroidRuntime(2316):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.URLConnectionHttpClient.post2_connect(URLConnectionHttpClient.java:409)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.URLConnectionHttpClient.post2(URLConnectionHttpClient.java:379)
10-07 15:13:39.975: E/AndroidRuntime(2316):     at winterwell.jtwitter.URLConnectionHttpClient.post(URLConnectionHttpClient.java:348)
10-07 15:13:39.975: E/AndroidRuntime(2316):     ... 15 more

我的代码是:

public void onClick(View v) {
        String statusText = editStatus.getText().toString();
        Twitter twitter = new Twitter("username", "password");
        twitter.setStatus(statusText);
        Log.d("StatusActivity","onClicked with text :"+statusText);
    }

我正在使用 jtwitter 版本 2.9.0

4

1 回答 1

1

你有NetworkOnMainThreadException.

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

您正在 ui 线程上运行与网络相关的操作。使用threadAsynctask

http://developer.android.com/reference/android/os/AsyncTask.html

          new TheTask().execute(); 
          // can pass parameters to doinbackground or
          // can pass param to constructor of asycntask

然后

 class TheTask extends AsyncTask<Void, Void, Void> {

    protected void doInBackground(Void... params) {
        try {
            // your twitter code here
            // do not update ui ere  
        } catch (Exception e) {


        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
    super.onPostExecute(result);    
    }
}
于 2013-10-07T04:20:05.593 回答