我正在尝试将照片和消息发布到用户的墙上。我的代码发布在下面。
当我发布消息时,它已成功发布。但是当我用图片发布消息时,我收到一个未知错误。
当用户按下按钮时,将执行以下代码:
mFacebook.authorize(MainActivity.this, new String[] { "user_about_me", "email","publish_stream", "read_stream", "offline_access" }, new TestLoginListener());
public class TestLoginListener implements DialogListener {
public void onComplete(Bundle values) {
try {
{
Bundle parameters = new Bundle();
parameters.putString("message", "Test\n");
String path=Environment.getExternalStorageDirectory()+"/Diegodeals/";
if(!new File(path).exists()) new File(path).mkdirs();
File photo = new File(path, "diegodeals.jpg");
if(photo!=null){
System.out.println(Uri.fromFile(photo).toString());
parameters.putString("picture", Uri.fromFile(photo).toString());
}
String response = mFacebook.request("me/feed", parameters, "POST");
JSONObject json;
try
{
json = Util.parseJson(response);
if (!json.isNull("id"))
{
Toast.makeText(MainActivity.this, "SUCCESSED ssssssss", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_LONG).show();
}
} catch (FacebookError e)
{
e.printStackTrace();
}
new FBLongOpertaion().execute("");
}
/*String resID = mFacebook.request("me");
JSONObject objId = Util.parseJson(response);*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void onCancel() {
}
public void onError(DialogError e) {
e.printStackTrace();
}
public void onFacebookError(FacebookError e) {
e.printStackTrace();
}
}
我收到了一个错误,例如检测到未知错误。我的Logcat值是
09-06 10:50:42.210: W/System.err(352): com.facebook.android.FacebookError: An unknown error has occurred.
09-06 10:50:42.240: W/System.err(352): at com.facebook.android.Util.parseJson(Util.java:279)
09-06 10:50:42.240: W/System.err(352): at com.eheuristics.android.diegodeals.MainActivity$TestLoginListener.onComplete(MainActivity.java:146)
09-06 10:50:42.240: W/System.err(352): at com.facebook.android.Facebook$1.onComplete(Facebook.java:354)
09-06 10:50:42.240: W/System.err(352): at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:145)
09-06 10:50:42.240: W/System.err(352): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
9-06 10:50:42.240: W/System.err(352): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
09-06 10:50:42.240: W/System.err(352): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 10:50:42.240: W/System.err(352): at android.os.Looper.loop(Looper.java:130)
09-06 10:50:42.240: W/System.err(352): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-06 10:50:42.251: W/System.err(352): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 10:50:42.251: W/System.err(352): at java.lang.reflect.Method.invoke(Method.java:507)
09-06 10:50:42.251: W/System.err(352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 10:50:42.251: W/System.err(352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 10:50:42.251:W / System.err(352):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在代码中使用以下行:parameters.putString(“access_token”,“”+ st);
facebook1 = new Facebook(APP_ID);
String st = get_token__(con);
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
parameters.putString("access_token", "" + st);
facebook1.request("me");
String response = facebook1.request("me/feed", parameters, "POST");
Log.d("Tests--->*************", "got response: " + response);
试试这可能对你有所帮助。
编辑回答
从这里下载代码:clickme
更改该代码中的APP_ID。
在share_text.java活动中更改以下方法
public static void postToWall(String message, Context con) {
facebook1 = new Facebook(APP_ID);
String st = get_token__(con);
Bundle parameters = new Bundle();
parameters.putString("caption", "I Like DJ Call Manager!" + "\n\n"
+ "https://www.facebook.com/DjCallManager");
parameters.putString("url",
"http://www.mspy.com/blog/wp-content/uploads/2012/04/1327425781_1.jpeg");
if (st.length() > 0) {
parameters.putString("access_token", "" + st);
}
try {
String response = facebook1
.request("me/photos", parameters, "POST");
Log.d("Tests--->*************", "got response: " + response);
if (response == null
|| response.equals("")
|| response.equals("false")
|| response
.equalsIgnoreCase("{\"error\":{\"message\":\"An active access token must be used to query information about the current user.\",\"type\":\"OAuthException\",\"code\":2500}}")) {
showToast("Blank response. please loginf again in facebook",
con);
clear_fb_data(con);
} else {
showToast("Message posted to your facebook wall!", con);
}
} catch (Exception e) {
showToast("Failed to post to wall!", con);
e.printStackTrace();
}
}
我使用网址分享照片只需更改该行即可共享文件代码。