Android-通过媒体Twitter发布状态更新&;Tumblr

Android-通过媒体Twitter发布状态更新&;Tumblr,android,twitter,oauth,upload,tumblr,Android,Twitter,Oauth,Upload,Tumblr,我正在尝试允许Android用户使用我的应用程序将图像发布到Twitter/Tumblr。我能够验证和检索用户和帐户信息,但我在实际图像上传方面遇到了问题。(基本上我对所有HTTP GET api调用都没有意见,但对HTTP POST没有意见) 我收到以下错误(分别为Twitter/Tumblr): 有人知道这是什么意思吗?我不认为这是身份验证错误,因为我可以获取用户信息等。。。在我看来,问题在于参数,可能是介质 我尝试了很多选择,包括使用图像文件/data/url、使用HttpParams/M

我正在尝试允许Android用户使用我的应用程序将图像发布到Twitter/Tumblr。我能够验证和检索用户和帐户信息,但我在实际图像上传方面遇到了问题。(基本上我对所有HTTP GET api调用都没有意见,但对HTTP POST没有意见)

我收到以下错误(分别为Twitter/Tumblr):

有人知道这是什么意思吗?我不认为这是身份验证错误,因为我可以获取用户信息等。。。在我看来,问题在于参数,可能是介质

我尝试了很多选择,包括使用图像文件/data/url、使用HttpParams/MultipartEntity和使用“media”/“media[]”,但没有太大成功。下面是我正在使用的当前代码。我的格式有问题吗?Twitter/Tumblr还需要什么吗?如果有人有任何想法、建议或改进,我们将不胜感激。谢谢

private class TwitterShareTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        HttpClient httpclient = GlobalValues.getHttpClient();
        HttpPost request = new HttpPost("https://api.twitter.com/1.1/statuses/update_with_media.json");

        try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("status", new StringBody(ETdescription.getText().toString()));
            entity.addPart("media[]", new FileBody(new File(GlobalValues.getRealPathFromURI(
                    Camera_ShareActivity.this, imageUri))));
            request.setEntity(entity);
            TwitterUtils.getTwitterConsumer().sign(request);

            HttpResponse response = httpclient.execute(request, GlobalValues.getLocalContext());
            HttpEntity httpentity = response.getEntity();
            InputStream instream = httpentity.getContent();
            result = GlobalValues.convertStreamToString(instream);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }

        return result;
    }

    public void onPostExecute(String result) {
        try {
            JSONObject jObject = new JSONObject(result.trim());
            System.out.println(jObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }           
    }
}
私有类TwitterShareTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串结果=”;
HttpClient HttpClient=GlobalValues.getHttpClient();
HttpPost请求=新建HttpPost(“https://api.twitter.com/1.1/statuses/update_with_media.json");
试一试{
多方实体=新多方实体();
entity.addPart(“状态”,新的StringBody(ETdescription.getText().toString());
entity.addPart(“media[]”,新文件体(新文件)(GlobalValues.getRealPathFromURI(
摄像头(ShareActivity.this,imageUri));
请求。设置实体(实体);
TwitterUtils.getTwitterConsumer().sign(请求);
HttpResponse response=httpclient.execute(请求,GlobalValues.getLocalContext());
HttpEntity HttpEntity=response.getEntity();
InputStream instream=httpentity.getContent();
结果=全局值。转换流到字符串(流内);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(OAuthMessageSignerException e){
e、 printStackTrace();
}捕获(OAutheExpectationFailedException e){
e、 printStackTrace();
}catch(OAuthCommunicationException e){
e、 printStackTrace();
}
返回结果;
}
public void onPostExecute(字符串结果){
试一试{
JSONObject jObject=新的JSONObject(result.trim());
系统输出打印项次(jObject);
}捕获(JSONException e){
e、 printStackTrace();
}           
}
}
~~~编辑:根据YuDroid的请求~~~

private static class TwitterUploadTask extends AsyncTask<String, Void, String> {

    private File image;
    private String message;
    private OAuthConsumer twitterConsumer;

    public TwitterUploadTask(OAuthConsumer consumer, File file, String string) {
        this.image = file;
        this.message = string;
        this.twitterConsumer = consumer;
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        HttpClient httpclient = GlobalValues.getHttpClient();
        HttpPost request = new HttpPost("https://api.twitter.com/1.1/statuses/update_with_media.json");

        ByteArrayInputStream bais = null;
        try {
            FileInputStream fis = new FileInputStream(image);
            BufferedInputStream bis = new BufferedInputStream(fis, 8192);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            fis.close();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] myTwitterByteArray = baos.toByteArray();
            bais = new ByteArrayInputStream(myTwitterByteArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("status", new StringBody(message));
            entity.addPart("media[]", new InputStreamBody(bais, image.getName()));
            request.setEntity(entity);
            twitterConsumer.sign(request);

            HttpResponse response = httpclient.execute(request, GlobalValues.getLocalContext());
            HttpEntity httpentity = response.getEntity();
            InputStream instream = httpentity.getContent();
            result = GlobalValues.convertStreamToString(instream);
            Log.i("statuses/update_with_media", result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }

        return result;
    }

    public void onPostExecute(String result) {
        try {
            JSONObject jObject = new JSONObject(result.trim());
            System.out.println(jObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }           
    }
}
私有静态类TwitterUploadTask扩展了AsyncTask{
私有文件图像;
私有字符串消息;
个人OAuthConsumer推特消费者;
公共TwitterUploadTask(OAuthConsumer使用者、文件文件、字符串){
this.image=文件;
this.message=字符串;
this.twitterConsumer=消费者;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串结果=”;
HttpClient HttpClient=GlobalValues.getHttpClient();
HttpPost请求=新建HttpPost(“https://api.twitter.com/1.1/statuses/update_with_media.json");
ByteArrayInputStream bais=null;
试一试{
FileInputStream fis=新的FileInputStream(图像);
BufferedInputStream bis=新的BufferedInputStream(fis,8192);
位图bm=BitmapFactory.decodeStream(bis);
二、关闭();
fis.close();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG,100,baos);
字节[]myTwitterByteArray=baos.toByteArray();
bais=新ByteArrayInputStream(myTwitterByteArray);
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
多方实体=新多方实体();
entity.addPart(“状态”,新的StringBody(消息));
entity.addPart(“media[]”,新的InputStreamBody(bais,image.getName());
请求。设置实体(实体);
twitterConsumer.签名(请求);
HttpResponse response=httpclient.execute(请求,GlobalValues.getLocalContext());
HttpEntity HttpEntity=response.getEntity();
InputStream instream=httpentity.getContent();
结果=全局值。转换流到字符串(流内);
Log.i(“状态/使用媒体更新”,结果);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(OAuthMessageSignerException e){
e、 printStackTrace();
}捕获(OAutheExpectationFailedException e){
e、 printStackTrace();
}catch(OAuthCommunicationException e){
e、 printStackTrace();
}
返回结果;
}
public void onPostExecute(字符串结果){
试一试{
JSONObject jObject=新的JSONObject(result.trim());
系统输出打印项次(jObject);
}捕获(JSONException e){
e、 printStackTrace();
}           
}
}

我已经解决了Twitter的问题,但我仍然面临Tumblr的问题。据我所知,Tumblr目前不支持MultipartEntity,因此我们必须使用NameValuePairs来添加参数。此外,Tumblr不支持本地URL(这很有意义),因此参数是“数据”,而不是“源”。目前,我正在将文件转换为位图,将位图转换为字节[],并将字节[]编码为字符串。我收到了{“响应”:{“错误”:[“上传照片时出错]},“meta”:{“msg”:“请求错误”,“状态”:400}。有什么想法吗?你是如何解决在Twitter上上传图片的问题的。
private static class TwitterUploadTask extends AsyncTask<String, Void, String> {

    private File image;
    private String message;
    private OAuthConsumer twitterConsumer;

    public TwitterUploadTask(OAuthConsumer consumer, File file, String string) {
        this.image = file;
        this.message = string;
        this.twitterConsumer = consumer;
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        HttpClient httpclient = GlobalValues.getHttpClient();
        HttpPost request = new HttpPost("https://api.twitter.com/1.1/statuses/update_with_media.json");

        ByteArrayInputStream bais = null;
        try {
            FileInputStream fis = new FileInputStream(image);
            BufferedInputStream bis = new BufferedInputStream(fis, 8192);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            fis.close();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] myTwitterByteArray = baos.toByteArray();
            bais = new ByteArrayInputStream(myTwitterByteArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("status", new StringBody(message));
            entity.addPart("media[]", new InputStreamBody(bais, image.getName()));
            request.setEntity(entity);
            twitterConsumer.sign(request);

            HttpResponse response = httpclient.execute(request, GlobalValues.getLocalContext());
            HttpEntity httpentity = response.getEntity();
            InputStream instream = httpentity.getContent();
            result = GlobalValues.convertStreamToString(instream);
            Log.i("statuses/update_with_media", result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }

        return result;
    }

    public void onPostExecute(String result) {
        try {
            JSONObject jObject = new JSONObject(result.trim());
            System.out.println(jObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }           
    }
}