У меня проблемы с этим сообщением в Facebook для моего приложения. Это идет так, как я ожидал, но не публикуется на моей стене. Ну вот мой код для поста:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.BaseRequestListener;
import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;
/**
* Created by pc on 11/19/13.
*/
public class PostToFacebook {
Facebook mFacebook;
private static final String APP_ID = "MY_APP_ID";
private ProgressDialog mProgress;
private Handler mRunOnUi = new Handler();
Context context;
public PostToFacebook(Context baseContext){
context = baseContext;
}
public void postToFacebook() {
mProgress = new ProgressDialog(context);
mFacebook = new Facebook(APP_ID);
try{
SessionStore.restore(mFacebook, context);
}catch (Exception e){
}
mProgress.setMessage("Posting ...");
mProgress.show();
try{
AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
Bundle params = new Bundle();
params.putString("message", "MESSAGE");
params.putString("name", "MESSAGE");
params.putString("caption", "MESSAGE");
params.putString("link", "URL_HERE");
params.putString("description", "MESSAGE");
params.putString("picture", "URL_HERE");
mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
Log.v("Been Here", "Done that");
}catch (Exception ex){
ex.printStackTrace();
Log.v("Been Here", "Problem");
}
}
private final class WallPostListener extends BaseRequestListener {
public void onComplete(final String response) {
mRunOnUi.post(new Runnable() {
public void run() {
mProgress.cancel();
Toast.makeText(context, "Posted to Facebook", Toast.LENGTH_SHORT).show();
}
});
}
}
}
Я совершенно потерян на этом. Что ж, для сообщения facebookAsync это:
public void request(final String graphPath,
final Bundle parameters,
final String httpMethod,
final RequestListener listener) {
new Thread() {
@Override public void run() {
try {
String resp = fb.request(graphPath, parameters, httpMethod);
listener.onComplete(resp);
} catch (FileNotFoundException e) {
listener.onFileNotFoundException(e);
} catch (MalformedURLException e) {
listener.onMalformedURLException(e);
} catch (IOException e) {
listener.onIOException(e);
}
}
}.start();
}
Способ запроса:
public String request(String graphPath, Bundle params, String httpMethod)
throws FileNotFoundException, MalformedURLException, IOException {
params.putString("format", "json");
if (isSessionValid()) {
params.putString(TOKEN, getAccessToken());
}
String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath
: RESTSERVER_URL;
return Util.openUrl(url, httpMethod, params);
}
И метод openURL:
public static String openUrl(String url, String method, Bundle params)
throws MalformedURLException, IOException {
// random string as boundary for multi-part http post
String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
String endLine = "\r\n";
OutputStream os;
if (method.equals("GET")) {
url = url + "?" + encodeUrl(params);
}
Log.d("Facebook-Util", method + " URL: " + url);
HttpURLConnection conn =
(HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("User-Agent", System.getProperties().
getProperty("http.agent") + " FacebookAndroidSDK");
if (!method.equals("GET")) {
Bundle dataparams = new Bundle();
for (String key : params.keySet()) {
if (params.get(key) instanceof byte[]) {
dataparams.putByteArray(key, params.getByteArray(key));
}
}
// use method override
if (!params.containsKey("method")) {
params.putString("method", method);
}
if (params.containsKey("access_token")) {
String decoded_token =
URLDecoder.decode(params.getString("access_token"));
params.putString("access_token", decoded_token);
}
conn.setRequestMethod("POST");
conn.setRequestProperty(
"Content-Type",
"multipart/form-data;boundary="+strBoundary);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
os = new BufferedOutputStream(conn.getOutputStream());
os.write(("--" + strBoundary +endLine).getBytes());
os.write((encodePostBody(params, strBoundary)).getBytes());
os.write((endLine + "--" + strBoundary + endLine).getBytes());
if (!dataparams.isEmpty()) {
for (String key: dataparams.keySet()){
os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
os.write(dataparams.getByteArray(key));
os.write((endLine + "--" + strBoundary + endLine).getBytes());
}
}
os.flush();
}
String response = "";
try {
response = read(conn.getInputStream());
} catch (FileNotFoundException e) {
// Error Stream contains JSON that we can parse to a FB error
response = read(conn.getErrorStream());
}
return response;
}
Чтобы добавить, я уже добавил расширенные разрешения publish_stream и publish_actions для настроек приложения.