【发布时间】:2014-01-10 21:32:45
【问题描述】:
使用 Android 应用程序,我正在尝试使用 graph-api 参考将照片发布到 facebook 墙上: https://developers.facebook.com/docs/graph-api/reference/user/photos/
Bundle params = new Bundle();
params.putString("source", "{image-data}");
/* make the API call */
new Request(
session,
"/me/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
我可以轻松地将图片传递图像 url 上传到“源”,但我想从我的 FileInputStream 发送 multipart/form-data 而不上传到服务器。
谁能解释我如何从“照片,编码为表单数据”中生成一个字符串? 这个方法我试过了,好像不行:
static final String GetMultipartFormData(InputStream fileInputStream)
{
StringBuilder sb = new StringBuilder();
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
sb.append(twoHyphens + boundary + lineEnd);
sb.append("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"fileName.jpg\"" + lineEnd);
sb.append(lineEnd);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
sb.append(lineEnd);
sb.append(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return sb.toString();
}
非常感谢。
【问题讨论】:
标签: android facebook facebook-graph-api publish multipartform-data