2

我正在使用 Graph API 将标记的图像上传到 facebook。这是我的代码:

MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            HttpClient httpClient = new DefaultHttpClient();
            mPostTagUrl = "https://graph.facebook.com/"
                    + dataManager.getOwnerId() + "/photos";

            HttpPost httppost = new HttpPost(mPostTagUrl);
            byte[] data = null;
            try {
                try {

                    String mPath = Environment.getExternalStorageDirectory()
                            .toString() + "/" + "screenshot.jpg";

                    FileInputStream fis = new FileInputStream(mPath);
                    Bitmap bi = BitmapFactory.decodeStream(fis);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    data = baos.toByteArray();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Log.d("onCreate", "debug error  e = " + e.toString());
                }
                if (data != null) {
                    ByteArrayBody bab = new ByteArrayBody(data, "event_pic"
                            + ".jpg");
                    entity.addPart("source", bab);

                }

                StringBody mAccesssToken = new StringBody(dataManager
                        .getAccessToken().trim());
                entity.addPart("access_token", mAccesssToken);
JSONArray mJsonArray = new JSONArray();
JSONObject json;
                for (int i = 0; i < getAllAddFriends().size(); i++) {
                    final int index = i;
                    String fId = getAllAddFriends().get(index)
                            .getFacebookFriendId();
                    Log.d("gaurav", "facebook id ==" + fId);
                    try {
                         json = new JSONObject();
                        json.put("x", "" + position);
                        json.put("y", "" + position);

                        json.put("tag_uid", fId);

                        position = position + 10;
                        mJsonArray.put(i, json);

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                String str = new String(mJsonArray.toString());
                Log.d("gaurav", "Add event Url=" + str);

                StringBody mEventNameBody = new StringBody(str);
                entity.addPart("tags", mEventNameBody);

                httppost.setEntity(entity);
                try {
                    httpClient.execute(httppost);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                HttpResponse response = httpClient.execute(httppost);
                String ret = EntityUtils.toString(response.getEntity());
                Log.d("gaurav", "Response Tag Friends =" + ret);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

请检查我的代码。我可以发布标记的图像,但在 facebook 中,相同的图像同时上传两次。请就这个问题向我提出建议。

4

1 回答 1

0

因为你执行了两次

httpClient.execute(httppost);

这就是为什么。

于 2013-02-21T08:50:42.067 回答