0

I am trying to integrate my application with social networks (facebook and twitter).

I want to have acess for exemple to a user wall or to a public wall and post it in my layout, like a scroll view or a item view (or any sugestions!!!), this is solved for twitter, but i can not put that work to facebook.

I use the Graph API.

To do a post in my wall i create the method 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;
    }
}

This method as listener for posting text messages, 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

    }

}

And them i just use a object of the facebook login to do a post in an onClick button

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

How can i do the same to get the feed from my wall or a public page? Thanks in advance!

4

1 回答 1

1

With some time around this problem, i and with a help of a coworker we had found the solution.

We will gonna create a HashMap pass it 2 two strings for example, one for the comments and another tothe author of the post. After we will gonna create a intent to another activity in the end of the method onComplete above, so this must look something like this:

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


    }

To the new activity, for example FacebookList we will create a following ItemList layout:

<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"/>

After that, we will gonna create the FacebookList Activity, create a array of the HashMap and a adapter:

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

I don't know if is the best approach but it worked for me, i hope that i help someone!

Best Regards

于 2012-06-04T14:44:58.073 回答