2

我使用以下代码发布带有文本的图像。它工作正常。

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(
                "https://graph.facebook.com/me/photos?access_token="
                        + accessToken);
        URL url = new URL("Some Image URL");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bi = BitmapFactory.decodeStream(input);

        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bi.compress(CompressFormat.PNG, 100, bos);
        byte[] data = bos.toByteArray();
        entity.addPart("source", new ByteArrayBody(data, "sample.png"));
        entity.addPart("message", new StringBody("Text need to be posted"));
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost,
                localContext);
        Log.v("response ", response + "");

在我的另一个活动中,我只需要发布消息(文本),所以我评论了这一行entity.addPart("source", new ByteArrayBody(data, "sample.png"));,文本不会发布在我的墙上。我如何修改此代码以仅发布文本

4

0 回答 0