在Android布局中从Facebook获取Feed

时间:2012-05-30 19:02:27

标签: android facebook facebook-graph-api

我正在尝试将我的应用程序与社交网络(Facebook和Twitter)集成。

我希望能够对用户墙或公共墙进行例证并将其发布到我的布局中,例如滚动视图或项目视图(或任何sugestions !!!),这是针对twitter解决的,但是我不能把这项工作交给facebook。

我使用的是图谱API。

要在我的墙上发帖,我创建方法 postTextOnMyWall

    public String postTextOnMyWall(String message) {

        Log.d("Tests", "Testing graph API wall post");
        try {
            Bundle parameters = new Bundle();
            parameters.putString("message", message);// key/value
            this.mAsyncRunner.request("me/feed", parameters, "POST",
                    new WallPostTextRequestListener(), null);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    public AsyncFacebookRunner getmAsyncRunner() {
        return this.mAsyncRunner;
    }

    public void setmAsyncRunner(AsyncFacebookRunner mAsyncRunner) {
        this.mAsyncRunner = mAsyncRunner;
    }
}

此方法作为发布短信的侦听器 WallPostTextRequestListener

    public class WallPostTextRequestListener implements RequestListener {

    public void showToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                .show();
    }

    // called on successful completion of the Request
    @Override
    public void onComplete(String response, Object state) {
        Log.d("WallPostTextRequestListener", "Got response: " + response);
        String message = "<empty>";
        try {
            JSONObject json = Util.parseJson(response);
            message = json.getString("message");
            showToast("Mensagem escrita no teu mural facebook!: " + message);
        } catch (JSONException e) {
            Log.e("WallPostTextRequestListener", "JSON Error in response");
        } catch (FacebookError e) {
            Log.e("WallPostTextRequestListener",
                    "Facebook Error: " + e.getMessage());
        }
        final String text = "Mensagem: " + message;
    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    // called if there is an error
    @Override
    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

}

他们只是使用facebook登录的对象在onClick按钮上发帖子

// this is the editext where i write my post
                String messageFace = ((EditText) findViewById(R.id.message))
                        .getText().toString();
                lf.postTextOnMyWall(messageFace);

如何从墙上或公共页面获取Feed? 提前致谢!

1 个答案:

答案 0 :(得分:1)

经过一段时间来解决这个问题,我在同事的帮助下找到了解决方案。

我们将创建一个HashMap传递它2两个字符串,例如,一个用于评论,另一个用于帖子的作者。在我们将在上面的onComplete方法的末尾创建另一个活动的意图之后,所以这必须看起来像这样:

public void onComplete(String response, Object state) {
        Log.d("getFeedfromWallRequestListener", "Got response: "
                + response);
        try {
            JSONObject json = Util.parseJson(response);
            Log.d("TESTE", "MY FEED EM JSON: " + json);

            String comments;
            String names;
        //Show all the comments and names
            JSONArray jArray=(JSONArray)json.get("data");
            for(int i=0;i<(jArray.length());i++)
            {
                 //jArray.getJSONObject(i).getJSONObject("from").get("name");   
                 //jArray.getJSONObject(i).get("message");
                HashMap<String, String> map = new HashMap<String, String>();

                 comments =   jArray.getJSONObject(i).get("message").toString();
                 names =   jArray.getJSONObject(i).getJSONObject("from").get("name").toString();
                    map.put("comments", comments);
                    map.put("names", names);
                    mylist.add(map);


                 Log.d("NAMES","Names: " + names);
                 Log.d("Comments","Comments: " + comments);

            }                       
            //showToast("Json com resposta do teu mural facebook!: " + json);
        } catch (JSONException e) {
            Log.w("getFeedfromWallRequestListener",
                    "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("getFeedfromWallRequestListener", "Facebook Error: "
                    + e.getMessage());
        }
        Intent intent = new Intent("android.intent.action.FacebookList");
        intent.putExtra("arraylist", mylist);

        try {
            Log.i("TAG", "starting activity(android.intent.action.FacebookList)");
            /*
             * 
             */
            startActivity(intent);
        } catch (Exception e) {
            Log.e("TAG", "error when trying to start activity: " + e.getMessage());
            for (int n = 0; n < e.getStackTrace().length; n++) {
                Log.e("TAG", "stack: " + e.getStackTrace()[n].toString());
            }
        }


    }

对于新活动,例如FacebookList,我们将创建以下ItemList布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">    

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="No data"/>

之后,我们将创建FacebookList Activity,创建一个HashMap数组和一个适配器:

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FacebookList extends ListActivity{

  /** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> arl =(ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
    System.out.println("...serialized data.."+arl);


    ListAdapter adapter = new SimpleAdapter(this, arl , R.layout.list_main, 
            new String[] { "comments", "names" }, 
            new int[] { R.id.item_title, R.id.item_subtitle });

    ListView listView = getListView();
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
}

我不知道这是否是最好的方法,但它对我有用,我希望我帮助别人!

最好的问候