3

我正在尝试允许 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}

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

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

0 回答 0