【问题标题】:Getting feeds from Facebook in Android layout在 Android 布局中从 Facebook 获取提要
【发布时间】:2012-05-30 19:02:26
【问题描述】:

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

我想访问例如用户墙或公共墙并将其发布到我的布局中,例如滚动视图或项目视图(或任何建议!!!),这已解决 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);

我怎样才能从我的墙或公共页面获取提要? 提前致谢!

【问题讨论】:

    标签: android facebook facebook-graph-api


    【解决方案1】:

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

    我们将创建一个 HashMap,例如将两个字符串传递给它,一个用于 cmets,另一个用于该帖子的作者。我们将在上面的 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();
            }
        });
    }
    }
    

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

    最好的问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 2017-09-10
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2014-04-19
      相关资源
      最近更新 更多