简体   繁体   English

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

[英]Why doesn't my call to the Facebook Graph API display anything?

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. 基本上,我希望用代表Facebook用户签到的标记填充Google Map。 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. 不幸的是,我的代码没有合作-我试图查看Facebook提供的文档并在网上搜索答案,而没有提出任何有用的建议。 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. 到目前为止,尽管我已经测试了在较早版本的应用程序中添加带有虚拟值的标记的功能,但我能使该应用程序执行的全部工作就是通过Facebook验证该应用程序的权限并显示地图。

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. 我之前的问题是关于为什么我对Graph API的调用没有显示任何内容-我进行了与AuthorizeListener子类中列出的相同的调用,但是只是尝试在日志条目中输出原始JSON字符串,而不是进行操作它。 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. 我认为我的代码使我有了一个很好的开端,但是AuthorizeListener子类中显然存在一些问题。 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

    }
}

} }

It might not be the reason but you haven't defined your app ID: 可能不是原因,但您尚未定义应用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: 另外,您必须在调用mFacebook.authorize的活动中覆盖onActivityResult,因此请将其添加到代码中:

    @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. 如果不这样做,则您的应用程序将不会获得Graph的令牌,并且您的连接将返回JSON错误消息msg。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM