如何在Java中使用httpcore将媒体上传到Twitter?

时间:2016-02-27 18:50:09

标签: java android twitter

使用twitter4j等打击我的dex限制;所以我必须使用httpcore在Java中滚动我自己的Twitter请求。 通过获取访问令牌并进行状态更新,我已经成功完成了所有工作。 现在我需要上传照片,所以我尝试了以下内容。

我已经查看过几十篇StackOverflow文章,以及Twitter自己的论坛,但我只是 找不到有用的帮助。

相关的Twitter文档是herehere,但它没有回答许多基本问题。

这是第一种方法:

BasicHttpEntityEnclosingRequest httpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest( POST, "https://upload.twitter.com/1.1/media/upload.json" );
// Since we setContentType("application/x-www-form-urlencoded"),
// it seems reasonable that we should send a UrlEncodedFormEntity.
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("command", "INIT"));
formparams.add(new BasicNameValuePair("media_type", "image/png"));
formparams.add(new BasicNameValuePair("total_bytes", "6046"));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, UTF_8);
httpEntityEnclosingRequest.setEntity( urlEncodedFormEntity );

这是第二种方法:

StringEntity stringEntity;
stringEntity = new StringEntity( "command=INIT&media_type=image%2Fjpeg&total_bytes=6046", UTF_8);
stringEntity.setContentType("application/x-www-form-urlencoded");
httpEntityEnclosingRequest.setEntity(stringEntity);

这两种方法都继续使用此代码:

urlEncodedFormEntity.setContentType("application/x-www-form-urlencoded");
httpEntityEnclosingRequest.setParams(httpParams);
httpEntityEnclosingRequest.addHeader("Authorization", authorization_header_string);
httpEntityEnclosingRequest.addHeader("Content-Type","x-www-form-urlencoded" );
HttpContext httpContext = getHttpContext(httpHost, httpClientConnection);
HttpResponse httpResponse = getHttpResponse( httpParams, httpClientConnection, httpContext, httpEntityEnclosingRequest);

两种方法都得到了回应: {&#34;错误&#34;:[{&#34;代码&#34;:38,&#34;消息&#34;:&#34;媒体参数丢失。&#34;}]}。< / p>

这段代码有什么问题?有人可以帮忙吗?

0 个答案:

没有答案