gpt4 book ai didi

android - 使用 Facebook Android SDK 3.0 上传照片时出现间歇性 SSL 错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:17 24 4
gpt4 key购买 nike

我正在使用 Facebook Android SDK 3.0 将照片上传到我的相册。一切正常,但有时第一次上传会因某种意外的 SSL 错误而失败。

参见 Request.java 中的函数 toHttpConnection .

try {
connection = createConnection(url);

serializeToUrlConnection(requests, connection);
} catch (IOException e) {
// THIS IS WHERE THE ERROR ORIGINATES FROM!
throw new FacebookException("could not construct request body", e);
} catch (JSONException e) {
throw new FacebookException("could not construct request body", e);
}

在 Eclipse 中将鼠标悬停在 e 上会显示以下信息:

javax.net.ssl.SSLException:写入错误:ssl=0x4f033008:系统调用期间发生 I/O 错误,管道损坏javax.net.ssl.SSLException:写入错误:ssl=0x4f033008:系统调用期间出现 I/O 错误,管道损坏写入错误:ssl=0x4f033008:系统调用期间出现 I/O 错误,管道损坏[1277468208, 0, 1279934152, 48, 1280860304, 26, 1280860480, 58, 1280859576, 11, 1280859696, 4, 1277492792, 1, 1280859640, 7, 1280233152, 169, 1280233264, 36, 1280230744, 6, 1280236632, 0, 1280236576 , 0, 1280238568, 6, 1280238512, 2, 1278731744, 21, 1279835640, 23, 1278097880, 2, 1279841920, 28, 1279843376, 2, 1277300032, 6]无

这是 SDK 中的错误吗?还有其他人遇到过这个吗?更重要的是,我该如何修复它?

最佳答案

在我看到这篇文章之前,我几乎准备接受我之前的(删除的)答案:http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

我决定试一试,它似乎奏效了!这是我的代码:

for (Bitmap b : bitmaps) {
// id is just a string
String response = executeUpload(b, id);

// do something with the response
}

private String executeUpload(Bitmap b, String id) throws MalformedURLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();

HttpPost postRequest = new HttpPost("https://graph.facebook.com/me/photos");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpClient httpClient = new DefaultHttpClient();
httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(2, true));

ByteArrayBody bab = new ByteArrayBody(data, id + ".png");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("access_token", new StringBody(activeFacebookAccessToken));
reqEntity.addPart("message", new StringBody(""));
reqEntity.addPart("picture", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String s;
StringBuilder builder = new StringBuilder();
while ((s = reader.readLine()) != null) {
builder = builder.append(s);
}

return builder.toString();
}

如以上链接所述,您必须安装最新的 HttpClient(位于 http://hc.apache.org/downloads.cgi )。

另一个需要注意的重要事项是 Facebook 在任一维度上支持的最大图像尺寸为 720 像素。在我的代码中,我在上传之前将位图缩放到该大小限制内,这大大提高了性能。

关于android - 使用 Facebook Android SDK 3.0 上传照片时出现间歇性 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com