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

发布于 2024-10-14 12:00:21 字数 5148 浏览 1 评论 0原文

好的,所以我正在编辑它以包含整个类以及我在过去几个小时添加的一些新代码。基本上,我希望用代表 Facebook 用户签到的标记来填充 Google 地图。不幸的是,我的代码没有配合 - 我尝试查看 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

    }
}

}

Ok, so I'm editing this to include the whole class with some new code I added over the past couple of hours. Basically, I'm looking to populate a Google Map with markers that represent a Facebook user's checkins. Unfortunately, my code has not been cooperating - I've tried reviewing the documentation that Facebook provides and searching the web for answers without coming up with anything useful. So far all I've been able to get the app to do is validate the app's permissions with Facebook and display the map, though I had tested the ability to add markers with dummy values in an earlier version of the app and that worked fine.

My earlier question dealt with why my calls to the Graph API weren't displaying anything - I had made the same call as listed in the AuthorizeListener sub-class, but was merely attempting to output the raw JSON string in a log entry instead of manipulating it. I think that whatever was the cause of that problem is probably the same cause of my current problem.

Anyway, how do I get my app to display markers for locations a user has checked in to? I think my code gets me off to a pretty good start, but there are obviously issues in my AuthorizeListener sub-class. What do you guys think?

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

    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-10-21 12:00:21

这可能不是原因,但您还没有定义您的应用程序 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 错误消息。

It might not be the reason but you haven't defined your app ID:

private static final String APP_ID = "";

Also, you have to override the onActivityResult in the activity where you call the mFacebook.authorize, so add this to your code:

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

If you don't do so, your app won't get the token for the Graph and your connection will return a JSON error msg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文