0
我正在嘗試創建一個Android應用程序,該應用程序會自動在用戶的Facebook牆上發送圖像。我已經在網上閱讀了很多例子(也來自這個網站),但顯然沒有任何東西可以用於我。這是我的代碼:Android:在Facebook上發佈圖像
class FacebookConnection extends AsyncTask<String, String, String> {
private Exception exception;
private Facebook facebook;
private static String APP_ID = "574586632609202"; // Replace your App ID here
Activity activity;
public FacebookConnection(Activity activity)
{
this.activity = activity;
}
private void login()
{
facebook = new Facebook(APP_ID);
facebook.authorize(activity, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},
new DialogListener() {
@Override
public void onComplete(Bundle values) {
}
@Override
public void onFacebookError(FacebookError error) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onCancel() {
}
});
}
protected String doInBackground(String... path) {
login();
Bundle parameters = new Bundle();
parameters.putString("message", "ciao");
Bitmap bi = BitmapFactory.decodeFile(path[0]);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
parameters.putByteArray("picture", byteArray);
String response = null;
try {
facebook.request("me");
response = facebook.request("me/feed", parameters, "POST");
} catch (MalformedURLException e) {
return e.getMessage();
} catch (IOException e) {
return e.getMessage();
}
if (response == null || response.equals(""))
return "No Response..";
else
return "Message has been posted to your walll!";
}
protected void onPostExecute(String mess) {
show("DONE", mess);
}
}
NB:我使用的按鈕點擊的主要活動是一流的,我通過我的主要活動作爲參數。方法「show」只是在消息框中寫入作爲參數傳遞的字符串。 看來,登錄是好的,但是當我檢查我的臉譜牆,沒有什麼改變。 謝謝你的幫助!
是的,我使用的AsyncTask避免NetworkOnMainThreadException ...我還沒有插入onPreExecute ......但doInBackground和onPostExecute在類裏面我已經貼!沒有? – user3025616
哦,我沒有看到你的代碼的其餘部分!咄! – hichris123
沒問題! ; ) 任何想法? – user3025616