2

我正在尝试从我的应用程序在用户墙上发帖而不显示提要对话框。我有一个与 anEditText getmessage和 a的活动Button with onClick()=postToWall。我正在关注这个问题: Android Facebook Post On Wall without Dialog Warnings

这是我的代码:

public void postToWall(View v){
        String message = getmessage.getText().toString();
        postMessage(message);       
    }

    public void postMessage(String message){
        Log.d("test", "testing post to wall");
        try{
            String response;
            Bundle parameters = new Bundle();
            parameters.putString("message", message);           
            response = facebook.request("me/feed", parameters, "POST");

            Log.d("test", "got response "+response);

            if(response == null || response.equals("")){
                Log.v("Error", "Blank");
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

以下是日志:

  06-06 20:03:19.726: I/ActivityManager(78): Displayed com.MyApp/.fbShare: +146ms
06-06 20:03:24.816: D/test(736): testing post to wall
06-06 20:03:24.828: W/System.err(736): android.os.NetworkOnMainThreadException
06-06 20:03:24.836: W/System.err(736):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-06 20:03:24.836: W/System.err(736):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-06 20:03:24.846: W/System.err(736):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-06 20:03:24.846: W/System.err(736):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
06-06 20:03:24.846: W/System.err(736):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-06 20:03:24.856: W/System.err(736):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-06 20:03:24.856: W/System.err(736):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-06 20:03:24.856: W/System.err(736):  at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
06-06 20:03:24.856: W/System.err(736):  at com.facebook.android.Util.openUrl(Util.java:193)
06-06 20:03:24.866: W/System.err(736):  at com.facebook.android.Facebook.request(Facebook.java:751)
06-06 20:03:24.866: W/System.err(736):  at com.MyApp.fbShare.postMessage(fbShare.java:56)
06-06 20:03:24.866: W/System.err(736):  at com.MyApp.fbShare.postToWall(fbShare.java:47)
06-06 20:03:24.866: W/System.err(736):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 20:03:24.866: W/System.err(736):  at java.lang.reflect.Method.invoke(Method.java:511)
06-06 20:03:24.876: W/System.err(736):  at android.view.View$1.onClick(View.java:3039)
06-06 20:03:24.876: W/System.err(736):  at android.view.View.performClick(View.java:3511)
06-06 20:03:24.876: W/System.err(736):  at android.view.View$PerformClick.run(View.java:14105)
06-06 20:03:24.876: W/System.err(736):  at android.os.Handler.handleCallback(Handler.java:605)
06-06 20:03:24.886: W/System.err(736):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 20:03:24.886: W/System.err(736):  at android.os.Looper.loop(Looper.java:137)
06-06 20:03:24.886: W/System.err(736):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 20:03:24.886: W/System.err(736):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 20:03:24.896: W/System.err(736):  at java.lang.reflect.Method.invoke(Method.java:511)
06-06 20:03:24.896: W/System.err(736):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 20:03:24.896: W/System.err(736):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 20:03:24.896: W/System.err(736):  at dalvik.system.NativeStart.main(Native Method)

我在这里错过了什么吗?我很感激任何帮助。谢谢!

4

1 回答 1

1

从 3.0(我认为)开始,如果您尝试在主(UI)线程上使用网络,系统会抛出异常。他们选择让系统这样做是为了鼓励开发人员不要将长时间运行的任务(例如网络操作)放在主线程上。

为了解决您的问题,您需要将您的网络操作(例如对 postMessage() 的调用)移动到后台线程。有几种方法。查看AsyncTask或查看使用Handler / Thread。如果您搜索“Android 创建后台线程”之类的内容,可以在网上找到许多示例

此外,由 Lars Vogel 制作的本教程非常适合学习这些内容。

于 2012-06-06T20:16:44.323 回答