【问题标题】:Android AsyncHttpClient - “Content-Type not allowed!” Regardless of types providedAndroid AsyncHttpClient - “Content-Type 不允许!”无论提供何种类型
【发布时间】:2013-11-26 00:34:06
【问题描述】:

大家好。

我第一次在 SO 上发帖,以前从未这样做过。阅读总是足够的。现在,我只是悲惨地坚持使用适用于 Android 的 AsyncHttpClient 库。除了下载文件之外,它在其他所有响应中都非常有效,至少在我被卡住的最后一天半内。 :-)

代码:

@Override
public void onClick(DialogInterface dialog, int which) {

    int userId = ((App)getActivity().getApplicationContext()).getUserId();

    String url = ZenApi.getAbsoluteUrl("api/attachments/" + userId + "/" +
            attachments[which]);
    selectedFile = attachments[which].toString();

    String[] allowedContentTypes = new String[] {"application/octet-stream",
        "text/html; charset=ISO-8859-1", "image/png", "image/jpeg",
        "image/bmp", "application/pdf", "text/html; charset=UTF-8", "image/png;charset=UTF-8" };

    BinaryHttpResponseHandler handler = new BinaryHttpResponseHandler(allowedContentTypes) {
        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
        };

        @Override
        public void onFinish() {
            ChooseMessageAttachmentFragment.this.dismiss();
            super.onFinish();
        }

        @Override
        public void onProgress(int bytesWritten, int totalSize) {
            // TODO Auto-generated method stub
            super.onProgress(bytesWritten, totalSize);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                byte[] binaryData, Throwable error) {
            Toast.makeText(getActivity(), "Failed to load file: " + error.getMessage(), Toast.LENGTH_LONG).show();
        }
        @Override
        public void onSuccess(int statusCode, byte[] binaryData) {
            File file = new File(getActivity().getFilesDir(), selectedFile);
            if (file.exists()) {
                try {
                    FileOutputStream stream = new FileOutputStream(file.getPath());
                    stream.write(binaryData[0]);
                    stream.close();

                    MimeTypeMap mimemap = MimeTypeMap.getSingleton();
                    String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
                    String type = mimemap.getMimeTypeFromExtension(ext);
                    if (type == null ) {
                        type = "*/*";
                    }

                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), type);
                    startActivity(intent);

                } catch (java.io.IOException e) {
                    Toast.makeText(getActivity(), "Error downloading file", Toast.LENGTH_SHORT).show();
                }
            }

        }

    };

    ZenApi.post(url, null, handler);

}

使用的 url 格式正确(我检查过),并且使用此库的所有其他调用(JSON、登录 POST)都可以正常工作。

我已经研究并尝试了所有我能想到的或通过谷歌搜索或在这个特定库上的点击以及这个错误,我已经尝试了几乎所有可以想象的 MimeType 来包含在调用中,仍然与同样的错误。我真的很想让这个工作,所以我不必重构我所有的代码来使用内置的 httpclients,这是我认为的下一步。我也很喜欢这个库。

  • 尝试下载一个简单的 png 文件。

  • 提供图像的后端 API 调用正在工作,并且已经使用不同的客户端(浏览器、Titanium 应用程序等)运行了一年。

  • 通过 SSL 连接

  • 对我们后端的所有其他调用(不涉及文件下载)都可以在这个库中正常工作。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android download android-async-http


    【解决方案1】:

    您可以准确检查后端返回的文件类型。只需像这样覆盖BinaryHttpResponseHandler 中的onFailure

    @Override 
    public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) 
    { 
        Log.e(TAG, "onFailure!"+ error.getMessage());
        for (Header header : headers)
        {
            Log.i(TAG, header.getName()+" / "+header.getValue());
        }
    }   
    

    这也可以为您提供其他信息来解决您的问题。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2011-06-29
      • 1970-01-01
      • 2019-10-20
      • 2010-11-06
      • 2018-10-14
      • 2019-12-15
      • 1970-01-01
      相关资源
      最近更新 更多