试图获取朋友列表正在返回未知的主机异常

时间:2012-08-27 17:05:01

标签: android facebook json

我想找一个朋友列表:

  private void getFriends(){

    JSONObject json_data = null;

    try
    {
        JSONObject response = Util.parseJson(facebook.request("me/friends")); // Get a friend information from facebook
        JSONArray jArray = response.getJSONArray("data");

        json_data = jArray.getJSONObject(0);
        String name = json_data.getString("name");
        Log.e("lovehate", name);


    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }

}

此代码命中IOEXception -

 08-27 18:00:29.630: D/Facebook-Util(5068): GET URL: https://graph.facebook.com/me/friends?access_token=[REMOVED_ACCESS_TOKEN]&format=json
08-27 18:00:32.390: W/System.err(5068): java.net.UnknownHostException: graph.facebook.com
08-27 18:00:32.400: W/System.err(5068):     at java.net.InetAddress.lookupHostByName(InetAddress.java:500)
08-27 18:00:32.400: W/System.err(5068):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:297)
08-27 18:00:32.400: W/System.err(5068):     at java.net.InetAddress.getAllByName(InetAddress.java:256)
08-27 18:00:32.400: W/System.err(5068):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)

我的权限列表如下所示:

 facebook.authorize(this, new String[] {}, new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                          Log.e("tag","new auth stored");

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();
                    }

2 个答案:

答案 0 :(得分:1)

堆栈跟踪显示您的设备/模拟器无法连接到我们的端点。确保您可以访问Internet,并且您的应用程序具有访问Internet的权限。

<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:-1)

您的字符串权限数组为空,我不知道这是不是您的问题。我正在使用email,publish_stream和user_photos。 此外,我使用此代码来获取朋友,这对我有用,你可以试试。

    Bundle params = new Bundle();
            access_token = fb.getAccessToken();

            try {
                params.putString("format", "json");
                params.putString("access_token", access_token);
                String url = "https://graph.facebook.com/me/friends";
                String response = Util.openUrl(url, "GET", params);
                JSONObject json = Util.parseJson(response);
                JSONArray jsonArray = json.optJSONArray("data");

                if(jsonArray != null) {
                    for(int i = 0; i < jsonArray.length(); i++) {
                        String tempName = jsonArray.getJSONObject(i).getString("name");
                        String tempId = jsonArray.getJSONObject(i).getString("id");
                        String location = jsonArray.getJSONObject(i).getString("location");
                        System.out.println(tempName + ", " + tempId + ", " + location);
                    }
                }

                //catch stuff