本文介绍了415不支持的MediaType错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTTPS POST请求将照片上传到服务器。请求是Multipart,我使用此代码上传图片。

I am uploading a Photo to the server using HTTPS POST request. The request is Multipart and I am using this code to upload the pic.

 public HttpResponse uploadPhoto(File imagePath) {

    try {

        HttpPost httppost = new HttpPost(
                "https://wbapi.cloudapp.net:443/api/Submission/UploadCompetitionEntry?folderName=photos");

        MultipartEntity multipartEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        httppost.setHeader( "Content-Type", "multipart/form-data" );
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Authorization", getAuth());
        multipartEntity.addPart("CompetitionID", new StringBody("84"));
        multipartEntity.addPart("UserId", new StringBody(""
                + LoginActivity.id_user));
        multipartEntity.addPart("Title", new StringBody(""
                + edit_title.getText().toString()));
        multipartEntity.addPart("Summary", new StringBody(""
                + edit_description.getText().toString()));
        multipartEntity.addPart("photos", new FileBody(imagePath,"image/jpeg"));
        multipartEntity.addPart("MediaType", new StringBody("Photo"));
        multipartEntity.addPart("Visibility", new StringBody("Public"));
        multipartEntity.addPart("DateCreated", new StringBody(
                "2013-01-04T10:07:46.9450000Z"));
        httppost.setEntity(multipartEntity);
        http = mHttpClient.execute(httppost);

    } catch (Exception e) {
        Log.v("Exception", "" + e.getMessage());
        e.printStackTrace();
    }
    return http;
}

我的HTTPClient就是这个

and my HTTPClient is this

mHttpClient = WinboutServerRequests.getHTTPClientObject();

这将使用Helper类来处理HTTPS证书。

This takes care of the HTTPS Certificate stuff with Helper classes.

当我正在执行请求时,我将e.getMessage()视为null并且在StackTrace中我得到了这个

When I am doing the request, I am getting e.getMessage() as null and in StackTrace I am getting this

我必须发送这些实体到服务器

I have to send these entities to the server

请求发送

答案:

有这个Authoris必须添加到缺少的标题中的一个。我添加了它,现在我没有得到CientProtocolException。

There was this Authorisation which has to be added to the header which was missing.I added it and Now I am not getting CientProtocolException.

添加了实用程序函数来获取文件的MIME类型

Added Utility Function to get the MIME Type of the file

public static String getMimeType(String url)
{
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

但我得到415不支持的媒体类型错误,我们必须设置用于上传图片的ContentType?

But I am getting 415 Unsupported Media Type error, What we have to set the ContentType for uploading Pictures ?

要发送的示例请求:

内容 - 处置:表格数据; name =CompetitionID

Content-Disposition: form-data; name="CompetitionID"

Content-Disposition:form-data; name =UserId

Content-Disposition: form-data; name="UserId"

Content-Disposition:form-data; name =Title

Content-Disposition: form-data; name="Title"

Content-Disposition:form-data; name =Summary

Content-Disposition: form-data; name="Summary"

Content-Disposition:form-data; name =MediaType

Content-Disposition: form-data; name="MediaType"

Content-Disposition:form-data; name =DateCreated

Content-Disposition: form-data; name="DateCreated"

内容 - 处置:表格 - 数据; name =Visibility

Content-Disposition: form-data; name="Visibility"

Content-Disposition:form-data; NAME = 照片; filename = IMG_9322.JPG

Content-Disposition: form-data; name="photos"; filename=IMG_9322.JPG

Content-Disposition:form-数据; NAME = 视频; filename = sample.avi

Content-Disposition: form-data; name="videos"; filename=sample.avi

Content-Disposition:form-数据; NAME = 音乐; filename = sample.avi

Content-Disposition: form-data; name="music"; filename=sample.avi

Content-Disposition:form-数据; NAME = 著作; filename = sample.txt
Content-Type:application / octet-stream

Content-Disposition: form-data; name="writings"; filename=sample.txtContent-Type:application/octet-stream

推荐答案

以下是类似的帖子你可以检查你的问题。

Here are the similar posts you can check out for your issue.

1)

2)

3)

4)

这篇关于415不支持的MediaType错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:29