Android - 发布状态更新与媒体Twitter& tumblr

时间:2013-08-05 23:15:48

标签: android twitter oauth upload tumblr

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

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

"response":{"errors":[{"message":"Error creating status","code":189}]}
"response":{"errors":["Error uploading photo."]},"meta":{"msg":"Bad Request","status":400}

有谁知道这意味着什么?我不相信这是一个身份验证错误,因为我能够获取用户信息等...它在我看来问题是参数,可能是媒体。

我尝试了很多选项,包括使用图像文件/ 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();
        }           
    }
}

~~~编辑:按照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();
        }           
    }
}

0 个答案:

没有答案