Post message with photo in facebook with android a

2019-08-17 01:42发布

I am trying to post a photo and a message to a user's wall. My code is posted below.

When I post just the message, it is successfully posted. But when I post message with a picture I get an Unknown error.

When user press on Button than below code will be execute:

     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();
    }
}

I got an error like UnKnown Error Detected. My Logcat value is

   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)

1条回答
来,给爷笑一个
2楼-- · 2019-08-17 02:02

Use this line in your code : 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);

try this it may help you.


edited answer

download code from here : clickme

change APP_ID in that code.

change below method in share_text.java Activity

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();
        }
    }

i used URL to share photo just change that line to share file code.

查看更多
登录 后发表回答