2012-05-30 55 views
0

我正在嘗試將我的應用程序與社交網絡(Facebook和Twitter)集成。在Android佈局中獲取來自Facebook的提要

我想要爲例如用戶牆或公共牆上的接口,並將其發佈在我的佈局中,如滾動視圖或項目視圖(或任何sugestions !!!),這是解決了Twitter的,但我不能把這項工作放到臉書上。

我使用Graph 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

隨着這個問題的一段時間,我和一個同事的幫助下,我們找到了解決方案。

我們將創建一個HashMap,例如傳遞它兩個字符串,一個用於評論,另一個用於該帖子的作者。之後,我們將要創建一個意圖中的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活動,創建的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(); 
     } 
    }); 
} 
} 

我不知道如果是最好的方法,但它對我有用,我希望我幫助別人!

最好的問候

+0

請給我完整的源代碼! – Divya