- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经搜索过 google 和 stackoverflow,但找不到解决方案。我正在尝试将 facebook 集成到我的应用程序中,以便用户可以通过我的应用程序直接向我的 facebook 粉丝页面推荐新的啤酒品牌。因为现在我每天收到大约 3 封电子邮件,用户会在其中推荐新的啤酒品牌。
我做了什么:- 创建了一个 facebook 开发者帐户,启用了原生 Android 应用程序并插入了 Key-Hash 等。- 下载并集成了facebook sdk。- 添加互联网权限- 集成了以下 facebook 帮助类:
package com.celticwolf.nsod; //changed
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
public class ShareOnFacebook extends Activity{
private static final String APP_ID = "35253892647899"; // changed
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());
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.facebook_dialog);
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"); // <------ here it fails
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();
}
}
并且我添加了以下代码来访问助手类:
private void shareonfb(){
Intent postOnFacebookWallIntent = new Intent(this, ShareOnFacebook.class);
postOnFacebookWallIntent.putExtra("facebookMessage", "Teeeeeeeeeeeest");
startActivity(postOnFacebookWallIntent);
}
失败的地方:- 我可以登录 facebook,但当它询问我是否要分享时,它在以下代码块处失败:
public void postToWall(String message){
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me"); // <------------- here it fails and jups to catch
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();
}
}
这是日志:(我已经标记了它失败的部分)
01-06 13:19:53.795: W/ActivityThread(1220): Application com.celticwolf.alex is waiting for the debugger on port 8100...
01-06 13:19:53.805: I/System.out(1220): Sending WAIT chunk
01-06 13:19:54.205: I/System.out(1220): Debugger has connected
01-06 13:19:54.205: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:54.405: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:54.615: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:54.820: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:55.250: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:55.450: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:55.650: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:55.850: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:56.050: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:56.250: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:56.450: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:56.650: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:56.850: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:57.050: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:57.255: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:57.455: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:57.655: I/System.out(1220): waiting for debugger to settle...
01-06 13:19:57.855: I/System.out(1220): debugger has settled (1373)
01-06 13:19:58.285: I/dalvikvm-heap(1220): Grow heap (frag case) to 7.205MB for 3502096-byte allocation
01-06 13:19:58.400: I/dalvikvm-heap(1220): Grow heap (frag case) to 13.139MB for 6224656-byte allocation
01-06 13:19:58.575: I/MediaPlayer(1220): setLPAflag() in
01-06 13:19:58.575: I/MediaPlayer(1220): mContext is null, can't getMirrorDisplayStatus!!!
01-06 13:19:58.575: I/MediaPlayer(1220): setLPAflag() out
01-06 13:19:58.585: W/MediaPlayer(1220): info/warning (1, 902)
01-06 13:19:58.585: D/MediaPlayer(1220): [DLNA]contentType = 902
01-06 13:19:58.585: D/MediaPlayer(1220): doStart() in
01-06 13:19:58.590: D/MediaPlayer(1220): getIntParameter = 902
01-06 13:19:58.665: D/MediaPlayer(1220): Mediaplayer receives message, message type: 200
01-06 13:19:58.665: I/MediaPlayer(1220): Info (1,902)
01-06 13:19:58.665: D/MediaPlayer(1220): Mediaplayer receives message, message type: 5
01-06 13:19:58.665: D/MediaPlayer(1220): Mediaplayer receives message, message type: 1
01-06 13:19:58.745: E/(1220): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
01-06 13:19:58.745: I/(1220): Attempting to load EGL implementation /system/lib//egl/libEGL_tegra_impl
01-06 13:19:58.805: I/(1220): Loaded EGL implementation /system/lib//egl/libEGL_tegra_impl
01-06 13:19:58.905: I/(1220): Loading GLESv2 implementation /system/lib//egl/libGLESv2_tegra_impl
01-06 13:19:59.350: D/MediaPlayer(1220): Mediaplayer receives message, message type: 2
01-06 13:20:00.200: D/MediaPlayer(1220): release() in
01-06 13:20:00.210: D/MediaPlayer(1220): release() out
01-06 13:20:00.745: W/MediaPlayer-JNI(1220): MediaPlayer finalized without being released
01-06 13:20:00.745: I/dalvikvm-heap(1220): Grow heap (frag case) to 15.433MB for 3317776-byte allocation
01-06 13:20:40.855: D/View(1220): ACTION_DOWN before UnsetPressedState. invoking mUnsetPressedState.run()
01-06 13:20:40.870: I/Choreographer(1220): Skipped 1158 frames! The application may be doing too much work on its main thread.
01-06 13:20:43.810: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:20:43.810: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:20:43.975: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:20:43.975: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:20:45.150: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:20:45.150: E/SpannableStringBuilder(1220): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-06 13:21:40.920: D/Facebook-Util(1220): GET URL: https://graph.facebook.com/me?access_token=AAAFAqIQXNsUBAE18DxmYZCvB9uLEFUSeQEMp3hZASDnjOMllu1Q0BqSTYEoGVEvjZAp8l18eZCt8ZArlPwQ5A08SijSFF00imS2JDO0A9MAZDZD&format=json
01-06 13:21:51.180: W/System.err(1220): android.os.NetworkOnMainThreadException <--- at this point it fails
01-06 13:21:51.215: W/System.err(1220): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208)
01-06 13:21:51.215: W/System.err(1220): at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
01-06 13:21:51.220: W/System.err(1220): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
01-06 13:21:51.220: W/System.err(1220): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-06 13:21:51.225: W/System.err(1220): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-06 13:21:51.225: W/System.err(1220): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-06 13:21:51.230: W/System.err(1220): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
01-06 13:21:51.230: W/System.err(1220): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-06 13:21:51.235: W/System.err(1220): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-06 13:21:51.235: W/System.err(1220): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-06 13:21:51.240: W/System.err(1220): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
01-06 13:21:51.240: W/System.err(1220): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
01-06 13:21:51.245: W/System.err(1220): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-06 13:21:51.245: W/System.err(1220): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-06 13:21:51.250: W/System.err(1220): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-06 13:21:51.255: W/System.err(1220): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-06 13:21:51.255: W/System.err(1220): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
01-06 13:21:51.255: W/System.err(1220): at com.facebook.android.Util.openUrl(Util.java:219)
01-06 13:21:51.260: W/System.err(1220): at com.facebook.android.Facebook.requestImpl(Facebook.java:806)
01-06 13:21:51.260: W/System.err(1220): at com.facebook.android.Facebook.request(Facebook.java:732)
01-06 13:21:51.265: W/System.err(1220): at com.celticwolf.alex.ShareOnFacebook.postToWall(ShareOnFacebook.java:81)
01-06 13:21:51.265: W/System.err(1220): at com.celticwolf.alex.ShareOnFacebook.share(ShareOnFacebook.java:68)
01-06 13:21:51.265: W/System.err(1220): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 13:21:51.265: W/System.err(1220): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 13:21:51.270: W/System.err(1220): at android.view.View$1.onClick(View.java:3603)
01-06 13:21:51.270: W/System.err(1220): at android.view.View.performClick(View.java:4101)
01-06 13:21:51.270: W/System.err(1220): at android.view.View$PerformClick.run(View.java:17078)
01-06 13:21:51.275: W/System.err(1220): at android.os.Handler.handleCallback(Handler.java:615)
01-06 13:21:51.275: W/System.err(1220): at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 13:21:51.275: W/System.err(1220): at android.os.Looper.loop(Looper.java:155)
01-06 13:21:51.280: W/System.err(1220): at android.app.ActivityThread.main(ActivityThread.java:5485)
01-06 13:21:51.280: W/System.err(1220): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 13:21:51.280: W/System.err(1220): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 13:21:51.285: W/System.err(1220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
01-06 13:21:51.285: W/System.err(1220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
01-06 13:21:51.285: W/System.err(1220): at dalvik.system.NativeStart.main(Native Method)
谢谢!
最佳答案
自 android 3.0 AFAIK 以来,您无法在主线程中进行网络操作。将您的调用放在单独的线程中,如果您需要更新 UI
,请使用 handlers
或 AsyncTask
例子:
class MyAsyncTask extends AsyncTask<String,Void,Boolean>
{
public Boolean doInBackground(String ...message){
Bundle parameters = new Bundle();
parameters.putString("message", message[0]);
parameters.putString("description", "topic share");
try {
facebook.request("me"); // <------------- here it fails and jups to catch
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
return Boolean.FALSE;
}
else {
return Boolean.TRUE;
}
} catch (Exception e) {
e.printStackTrace();
return Boolean.FALSE;
}
}
public void onPostExecute(Boolean result){
if(result == Boolean.TRUE){
showToast("posted successfully");
}else{
showToast("couldn't post to FB.");
}
finish();
}
}
关于android - 发布到 Facebook 墙时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14181653/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!