获取facebook个人资料图片url android时出错

时间:2013-10-11 10:22:50

标签: java android facebook facebook-graph-api

我想获取用户的Facebook个人资料图片网址,以下是代码。

代码:

Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {

                                StringBuilder res = new StringBuilder();

                                // fetch user profile picture url
                                URL url = null;
                                HttpURLConnection httpconn = null;
                                String strUrl = "http://graph.facebook.com/"
                                        + fbuser.getFacebookId()
                                        + "/picture?width=350&height=350";

                                try {
                                    url = new URL(strUrl);
                                    httpconn = (HttpURLConnection) url
                                            .openConnection();

                                } catch (MalformedURLException e1) {
                                    e1.printStackTrace();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                try {
                                    if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {

                                        BufferedReader input = new BufferedReader(
                                                new InputStreamReader(httpconn
                                                        .getInputStream()));
                                        String strLine = null;
                                        while ((strLine = input.readLine()) != null) {
                                            res.append(strLine);
                                        }
                                        input.close();
                                    }

                                    Log.e(TAG, "res : " + res);
                                    JSONObject imageUrlObject = new JSONObject(
                                            res.toString());

                                    fbuser.setImageUrl(imageUrlObject
                                            .getJSONObject("picture")
                                            .getJSONObject("data")
                                            .getString("url"));


                                    // Call a method of an Activity to notify
                                    // user info is received
                                    ((RS_LoginActivity) RS_Facebook.this.activity)
                                            .FacebookUserInfoReceived();

                                    // call login api

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

                            }
                        });
                        thread.start();

我记录字符串响应,下面是该响应的显示方式的屏幕截图。

enter image description here

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

API规范可能会发生变化;使用以下解决方法:

 fbuser.setImageUrl("http://graph.facebook.com/"
                                    + fbuser.getFacebookId()
                                    + "/picture?width=350&height=350");

该网址最近为您提供了JPEG图片,因此很可能是您需要的网址。

答案 1 :(得分:0)

public class Profile extends Activity {

    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_profile);

        iv= (ImageView)findViewById(R.id.imageView1);

         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
         StrictMode.setThreadPolicy(policy);
         String url = "http://graph.facebook.com/" + fbID+ "/picture?width=800&height=600";
         BitmapFactory.Options bmOptions;
         bmOptions = new BitmapFactory.Options();
         bmOptions.inSampleSize = 1;
         Bitmap bm = loadBitmap(url, bmOptions);
         iv.setImageBitmap(bm);

    }



     public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
            Bitmap bitmap = null;
            InputStream in = null;
            try 
            {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in, null, options);
                in.close();
            } 
            catch (IOException e1) {
            }
            return bitmap;
        }

     private static InputStream OpenHttpConnection(String strURL)
                throws IOException {
            InputStream inputStream = null;
            URL url = new URL(strURL);
            URLConnection conn = url.openConnection();

            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setRequestMethod("GET");
                httpConn.connect();

                if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputStream = httpConn.getInputStream();
                }
            } catch (Exception ex) {
            }
            return inputStream;
        }