-1

我想获取用户的 facebook 个人资料图片 url,下面是代码。

代码 :

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();

我记录了字符串响应,下面是该响应的屏幕截图。

在此处输入图像描述

知道为什么会这样吗?

4

2 回答 2

0

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

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

该 URL 是最近为您提供 JPEG 图像的原因,因此它很可能是您需要的 URL。

于 2013-10-11T10:29:13.077 回答
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;
        }
于 2013-10-11T11:00:00.317 回答