2

Good Day all.

My first time posting on SO, never had to before. Reading was always enough. Now, I'm just woefully stuck using AsyncHttpClient library for Android. It works fabulously in every other response besides downloading files, at least for me over the last day and a half of being stuck. :-)

Code:

@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);

}

The url used is correctly formed (I checked) and every other call using this library (JSON, Login POSTs) all work fine.

I've researched and tried everything that I can think of or have read through google searches or SO hits on this particular library in conjunction with this error and I have tried just about every imaginable MimeType to include to the call, still with the same error. I really would like to get this to work so I don't have to refactor all my code to use built in httpclients which is my next step I think. I also like this library very much.

  • Trying to download a simple png file.

  • The backend API call that serves up the image is working and has been for a year with different clients (browser, Titanium App, etc).

  • Connection is over SSL

  • All other calls (not involving downloading of files) to our backend work correctly with this library.

Any help would be appreciated. Thanks!

4

1 回答 1

1

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

@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());
    }
}   

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

希望这可以帮助

于 2014-01-16T11:41:39.763 回答