Facebook返回null对象

时间:2014-09-12 05:24:23

标签: android facebook-graph-api

我是android编程的新手。我第一次从Facebook获取数据。但是如果我登录并等待大约2-3分钟,则Object返回null。我使用Facebook类而不是Facebook SDK和doinbackground()方法的问题所以post()方法不执行。所以任何人告诉我我的代码有什么问题。

提前致谢。

下一个Activity.java

 package com.example.facebookdemo;


 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.MalformedURLException;
  import java.util.ArrayList;

 import org.json.JSONArray;
 import org.json.JSONObject;

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.ProgressDialog;
 import android.content.DialogInterface;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ListView;
 import android.widget.Toast;

 import com.Facebook.Facebook;
 import com.Facebook.FacebookError; 
 import com.Facebook.Util;
 import com.example.adapter.CustomBaseAdapter;
 import com.example.model.RowItems;

 public class NextActivity extends Activity{

ListView lv;
Facebook mFacebook;
//AsyncFacebookRunner mAsyncRunner;
ProgressDialog mProgress;
String postId,response,messages;
ArrayList<RowItems> results ;
String msg;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    lv = (ListView) findViewById(R.id.listView1);
    mFacebook = new Facebook(MainActivity.APP_ID);
    String token = mFacebook.getAccessToken();
    results=new ArrayList<RowItems>();
    new doInBackground().execute();

}

public class doInBackground extends AsyncTask<Void, Void, Void>{

   @Override
    protected void onPreExecute() {
        mProgress = ProgressDialog.show(NextActivity.this, "Facebook Friends",
                "Fetching Data", true, true);
    }; 

    @Override
    protected Void doInBackground(Void... params) {
        getSearchResults();

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        mProgress.dismiss();
        lv.setAdapter(new CustomBaseAdapter(NextActivity.this,results));
    }

}



private void getSearchResults(){

     Bundle parameters = new Bundle();
     parameters.putString("message", msg);
     try {
         String response = mFacebook.request("me?fields=home.limit(10)", parameters,
                 "GET");
         System.out.println(response);

            JSONObject jsonObject=null;

            try {
                jsonObject = Util.parseJson(response);
                Log.i("json Response", jsonObject.toString());
            } catch (FacebookError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            JSONArray ja= jsonObject.getJSONObject("home").getJSONArray("data");
                for(int i=0;i<=ja.length();i++){
                JSONObject jo = ja.getJSONObject(i);
                 RowItems items=new RowItems();
                 postId = jo.getString("id");

                JSONObject jo1 = jo.getJSONObject("from");
                String userProfileID = jo1.getString("id");
                String name = jo1.getString("name");
                 messages = jo.getString("message");

                if (userProfileID.equals(name)) {
                    items.setName(name);            
                    } else if (userProfileID.equals(messages)) {
                     items.setMessages(messages);

                }
                //list_ProfilePic.add("http://graph.facebook.com/"+userProfileID+"/picture?type=large");
                 results.add(items);
                }

        } catch (Exception e) {
            // TODO: handle exception
        }


   }
}

这是我的json

    {
"home": {
"data": [
  {
    "id": "514806045331953_533082596837631", 
    "from": {
      "name": "Yunus Bobotohpersib Jurigpersibcikampekbarat", 
      "id": "514806045331953"
    }, 
    "message": "Off hela arek dahar :)\n\n#freeadySuee", 
    "actions": [
      {
        "name": "Comment", 
        "link": "https://www.facebook.com/514806045331953/posts/533082596837631"
      }, 
      {
        "name": "Like", 
        "link": "https://www.facebook.com/514806045331953/posts/533082596837631"
      }
    ], 
    "privacy": {
      "value": ""
    }, 
    "type": "status", 
    "status_type": "mobile_status_update", 
    "application": {
      "name": "Facebook for Every Phone", 
      "id": "139682082719810"
    }, 
    "created_time": "2014-09-06T03:42:15+0000", 
    "updated_time": "2014-09-06T03:42:15+0000", 
    "likes": {
      "data": [
        {
          "id": "541839042584928", 
          "name": "Libra Gembelgembelsoten Anakdongo"
        }, 
        {
          "id": "1478461745742208", 
          "name": "Neng Ikha"
        }, 
        {
          "id": "611873252262908", 
          "name": "R'agil Putra"
        }, 
        {
          "id": "1661741794050418", 
          "name": "Norma C'nax Duchiietea GhajaNhimasta"
        }, 
        {
          "id": "506133499521977", 
          "name": "Wina Cuttegirl"
        }, 
        {
          "id": "1462738317325223", 
          "name": "Jamal S'boedakholigancikampek"
        }, 
        {
          "id": "656484581115919", 
          "name": "Mien Putrii Laviigne"
        }, 
        {
          "id": "427256690746224", 
          "name": "Puspa Echa Andani"
        }, 
        {
          "id": "843251385685549", 
          "name": "Imam Joe"
        }, 
        {
          "id": "1494528947454941", 
          "name": "Nani Maryani"
        }, 
        {
          "id": "245675808975948", 
          "name": "Zhuan Fals Duameniitsepuluhdetik"
        }, 
        {
          "id": "270769306466270", 
          "name": "Siti Munajah"
        }, 
        {
          "id": "286452664887250", 
          "name": "Rendi Syahputra"
        }, 
        {
          "id": "332245536951320", 
          "name": "Yuyun Latifahc"
        }, 
        {
          "id": "493466714113744", 
          "name": "Cacing Uwiss Warass"
        }, 
        {
          "id": "1469440296660772", 
          "name": "Syaie Whardhanie"
        }, 
        {
          "id": "1457472927860947", 
          "name": "Imma Nur Adima"
        }, 
        {
          "id": "662163223890967", 
          "name": "Indah Fije"
        }, 
        {
          "id": "296274043911531", 
          "name": "Rena C'cwcuttethea"
        }, 
        {
          "id": "329340663893934", 
          "name": "Aplez Sibocah Falsyangterlupakan"
        }, 
        {
          "id": "1451779098419632", 
          "name": "Radieth BomberMaungbiroe"
        }, 
        {
          "id": "627163187401324", 
          "name": "Mbi Semoks"
        }, 
        {
          "id": "708875802532701", 
          "name": "Gua TuchRespector Persibkabehdoeloer"
        }, 
        {
          "id": "348686475290235", 
          "name": "Rika Nur"
        }, 
        {
          "id": "1517480285132215", 
          "name": "Icha"
        }
      ], 
      "paging": {
        "cursors": {
          "after": "MTUxNzQ4MDI4NTEzMjIxNQ==", 
          "before": "NTQxODM5MDQyNTg0OTI4"
        }, 
        "next": "https://graph.facebook.com/v2.1/100004076851807_533082596837631/likes?         limit=25&after=MTUxNzQ4MDI4NTEzMjIxNQ=="
      }
    }
   }

1 个答案:

答案 0 :(得分:0)

以这种方式试试

更改此行:

mFacebook.request("me?fields=home.limit(10)", parameters,
                 "GET");

这两行:

Request request = mFacebook.request("me?fields=home.limit(10)", parameters,
                 "GET");
Response response = request.executeAnWait();