为什么我对Facebook Graph API的调用没有显示任何内容?

时间:2011-01-24 21:03:50

标签: java android facebook-graph-api facebook-android-sdk

好的,所以我正在编辑这个,以便在整个班级中加入我在过去几个小时内添加的一些新代码。基本上,我希望使用代表Facebook用户签到的标记来填充谷歌地图。不幸的是,我的代码没有合作 - 我已经尝试审查Facebook提供的文档,并在网上搜索答案,而没有提出任何有用的东西。到目前为止,我已经能够让应用程序做的是验证应用程序的权限与Facebook并显示地图,虽然我已经测试了在应用程序的早期版本中添加带虚拟值的标记的功能,并且工作正常。

我之前的问题涉及为什么我对Graph API的调用没有显示任何内容 - 我进行了与AuthorizeListener子类中列出的相同的调用,但只是尝试在日志条目中输出原始JSON字符串而不是操纵它。我认为造成这个问题的原因可能与我目前的问题原因相同。

无论如何,如何让我的应用显示用户已签到位置的标记?我认为我的代码让我有了一个非常好的开始,但我的AuthorizeListener子类中显然存在问题。你们觉得怎么样?

public class FBCTActivity extends MapActivity {
public static Context mContext;
List<Overlay> mapOverlays;
FBCTMarkerOverlay markerLayer;
ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();

// Facebook Application ID
private static final String APP_ID = "";

Facebook mFacebook = new Facebook(APP_ID);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.main);

    // Set up Facebook stuff
    mFacebook.authorize(this, new String[]{"user_checkins", "offline_access"}, new AuthorizeListener());

    // Set up map stuff
    MapView mMapView = (MapView)findViewById(R.id.map);
    mMapView.setSatellite(true);
    MapController mMapController = mMapView.getController();
    mMapController.animateTo(getCurrentLocation());
    mMapController.setZoom(3);

    // Set up overlay stuff
    mapOverlays = mMapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
    markerLayer = new FBCTMarkerOverlay(drawable);

    // markerLayer is populated in the AuthorizeListener sub-class
    mapOverlays.add(markerLayer);

}

/**
 * Determines the device's current location, but does not display it.
 * Used for centering the view on the device's location.
 * @return A GeoPoint object that contains the lat/long coordinates for the device's location.
 */
private GeoPoint getCurrentLocation() {
    LocationManager mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria mCriteria = new Criteria();
    mCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
    mCriteria.setPowerRequirement(Criteria.POWER_LOW);
    String mLocationProvider = mLocationManager.getBestProvider(mCriteria, true);
    Location mLocation = mLocationManager.getLastKnownLocation(mLocationProvider);

    int mLat = (int)(mLocation.getLatitude()*1E6);
    int mLong = (int)(mLocation.getLongitude()*1E6);
    return new GeoPoint(mLat, mLong);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private class AuthorizeListener implements DialogListener {
    public void onComplete(Bundle values) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String response = mFacebook.request("me/checkins"); // The JSON to get
                                            JSONObject jObject = Util.parseJson(response);
                    JSONArray jArray = jObject.getJSONArray("data"); // Read the JSON array returned by the request
                    for (int i = 0; i < jArray.length(); i++) { // Iterate through the array
                        JSONObject outerPlace = jArray.getJSONObject(i); // The outer JSON object
                        JSONObject place = outerPlace.getJSONObject("place"); // Second-tier JSON object that contains id, name, and location values for the "place"
                        String placeName = place.getString("name"); // The place's name
                        JSONObject placeLocation = place.getJSONObject("location"); // Third-tier JSON object that contains latitude and longitude coordinates for the place's "location"
                        int lat = (int) (placeLocation.getDouble("latitude")*1E6); // The place's latitude
                        int lon = (int) (placeLocation.getDouble("longitude")*1E6); // The place's longitude
                        String date = outerPlace.getString("created_time"); // Timestamp of the checkin
                        overlays.add(new OverlayItem(new GeoPoint(lat, lon), placeName, "Checked in on: " + date)); // Add the place's details to our ArrayList of OverlayItems
                    }
                    mFacebook.logout(mContext); // Logout of Facebook
                    for (int i = 0; i < overlays.size(); i++) {
                        markerLayer.addOverlayItem(overlays.get(i));
                    }
                } catch(IOException e) {
                    Log.v("FBCTActivity", e.getMessage());
                } catch(JSONException e) {
                    Log.v("FBCTActivity", e.getMessage());
                }
            }
        }.start();
    }

    public void onFacebookError(FacebookError e) {
        Log.w("FBCTActivity", e.getMessage());
        // TODO: Add more graceful error handling
    }

    public void onError(DialogError e) {
        Log.w("FBCTActivity", e.getMessage());
    }

    public void onCancel() {
        // TODO Auto-generated method stub

    }
}

}

1 个答案:

答案 0 :(得分:0)

可能不是原因,但您尚未定义应用ID:

private static final String APP_ID = "";

此外,您必须覆盖调用mFacebook.authorize的活动中的onActivityResult,因此请将其添加到您的代码中:

    @Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

如果您不这样做,您的应用将无法获取图表的令牌,您的连接将返回JSON错误信息。