我已经在android for android android integaration中制作了一个简单的程序,我尝试过如下:它显示了facebook登录对话框并成功登录后,它还显示了myapp并要求与我的facebook朋友分享但是当我按“是”..它说未能贴在墙上“....请告诉我这是什么问题, 我的代码如下:
main.java
package com.example.facbk;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import android.net.ParseException;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String APP_ID = "454641867918042";
private static final String[] PERMISSIONS = new String[] {"publish_stream"};
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private String messageToPost;
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
System.out.println("hello token");
System.out.println(facebook.getAccessToken());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null){
facebookMessage = "Test wall post";
}
messageToPost = facebookMessage;
}
public void doNotShare(View button){
finish();
}
public void share(View button){
if (! facebook.isSessionValid()) {
loginAndPostToWall();
}
else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
public void postToWall(String message){
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
showToast("Blank response.");
}
else {
showToast("Message posted to your facebook wall!");
}
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null){
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
private void showToast(String message){
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
logcat的
07-11 18:45:55.200: E/libEGL(1006): call to OpenGL ES API with no current context (logged once per thread)
07-11 18:45:55.200: D/ShaderProgram(1006): couldn't load the vertex shader!
07-11 18:45:55.211: E/libEGL(1006): call to OpenGL ES API with no current context (logged once per thread)
07-11 18:45:55.211: D/ShaderProgram(1006): couldn't load the vertex shader!
07-11 18:45:55.211: E/libEGL(1006): call to OpenGL ES API with no current context (logged once per thread)
07-11 18:45:55.211: D/ShaderProgram(1006): couldn't load the vertex shader!
07-11 18:45:55.211: E/libEGL(1006): call to OpenGL ES API with no current context (logged once per thread)
07-11 18:45:55.221: D/ShaderProgram(1006): couldn't load the vertex shader!
07-11 18:45:55.221: E/libEGL(1006): call to OpenGL ES API with no current context (logged once per thread)
07-11 18:45:55.221: D/ShaderProgram(1006): couldn't load the vertex shader!
07-11 18:46:12.610: D/FacebookSDK.WebDialog(1006): Webview loading URL: https://m.facebook.com/dialog/oauth/confirm
07-11 18:46:14.720: D/FacebookSDK.WebDialog(1006): Redirect URL: fbconnect://success#access_token=CAAGdfo8JitoBAEAs1rWo2oDZCG4JVSeoqV4HxmZBLHkrVgZCjDlX07bFVMy7fOmRA1VbNXTC0oTPNVMagcZCbXS3wQsA4bAkoEwdd7atG8VKZBOYR6ClAFZCYZAVDGUpz1kbMTLAl99VauZAzXswbsBe&expires_in=5157278
07-11 18:46:15.370: D/dalvikvm(1006): GC_CONCURRENT freed 427K, 6% free 9524K/10055K, paused 7ms+26ms
07-11 18:46:15.831: I/System.out(1006): hello token
07-11 18:46:15.831: I/System.out(1006): CAAGdfo8JitoBAEAs1rWo2oDZCG4JVSeoqV4HxmZBLHkrVgZCjDlX07bFVMy7fOmRA1VbNXTC0oTPNVMagcZCbXS3wQsA4bAkoEwdd7atG8VKZBOYR6ClAFZCYZAVDGUpz1kbMTLAl99VauZAzXswbsBe
07-11 18:46:15.910: D/Facebook-Util(1006): GET URL: https://graph.facebook.com/me?access_token=CAAGdfo8JitoBAEAs1rWo2oDZCG4JVSeoqV4HxmZBLHkrVgZCjDlX07bFVMy7fOmRA1VbNXTC0oTPNVMagcZCbXS3wQsA4bAkoEwdd7atG8VKZBOYR6ClAFZCYZAVDGUpz1kbMTLAl99VauZAzXswbsBe&format=json
07-11 18:46:16.030: W/System.err(1006): android.os.NetworkOnMainThreadException
07-11 18:46:16.061: W/System.err(1006): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-11 18:46:16.061: W/System.err(1006): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-11 18:46:16.061: W/System.err(1006): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-11 18:46:16.070: W/System.err(1006): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-11 18:46:16.070: W/System.err(1006): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
07-11 18:46:16.070: W/System.err(1006): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-11 18:46:16.081: W/System.err(1006): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
07-11 18:46:16.081: W/System.err(1006): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
07-11 18:46:16.081: W/System.err(1006): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-11 18:46:16.081: W/System.err(1006): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
07-11 18:46:16.091: W/System.err(1006): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
07-11 18:46:16.091: W/System.err(1006): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
07-11 18:46:16.091: W/System.err(1006): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
07-11 18:46:16.091: W/System.err(1006): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
07-11 18:46:16.100: W/System.err(1006): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-11 18:46:16.110: W/System.err(1006): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-11 18:46:16.110: W/System.err(1006): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
07-11 18:46:16.120: W/System.err(1006): at com.facebook.android.Util.openUrl(Util.java:219)
07-11 18:46:16.120: W/System.err(1006): at com.facebook.android.Facebook.requestImpl(Facebook.java:806)
07-11 18:46:16.130: W/System.err(1006): at com.facebook.android.Facebook.request(Facebook.java:732)
07-11 18:46:16.130: W/System.err(1006): at com.example.facbk.MainActivity.postToWall(MainActivity.java:93)
07-11 18:46:16.130: W/System.err(1006): at com.example.facbk.MainActivity$LoginDialogListener.onComplete(MainActivity.java:115)
07-11 18:46:16.140: W/System.err(1006): at com.facebook.android.Facebook.onSessionCallback(Facebook.java:345)
07-11 18:46:16.140: W/System.err(1006): at com.facebook.android.Facebook.access$11(Facebook.java:326)
07-11 18:46:16.140: W/System.err(1006): at com.facebook.android.Facebook$1.call(Facebook.java:304)
07-11 18:46:16.151: W/System.err(1006): at com.facebook.Session$3$1.run(Session.java:1190)
07-11 18:46:16.151: W/System.err(1006): at android.os.Handler.handleCallback(Handler.java:605)
07-11 18:46:16.151: W/System.err(1006): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 18:46:16.160: W/System.err(1006): at android.os.Looper.loop(Looper.java:137)
07-11 18:46:16.160: W/System.err(1006): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-11 18:46:16.171: W/System.err(1006): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:46:16.181: W/System.err(1006): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 18:46:16.181: W/System.err(1006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 18:46:16.191: W/System.err(1006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 18:46:16.191: W/System.err(1006): at dalvik.system.NativeStart.main(Native Method)
07-11 18:46:19.750: I/jdwp(1006): Ignoring second debugger -- accepting and dropping
答案 0 :(得分:2)
您的代码存在两个问题。
首先,您使用的是Facebook SDK的弃用版本。如果您使用的是SDK的3.0.x,请不要使用Facebook类,而是使用Session和Request类。有关教程和示例,请参阅https://developers.facebook.com/android/。但这实际上并没有影响到正确性,这让我感到...
您的问题是您正在主线程上执行网络请求(正如异常所说)。你需要做的是将你的Facebook.request调用包装在AsyncTask的doInBackground方法中。