尝试从Android应用程序上传图像文件到服务器时出错

时间:2017-01-31 14:43:17

标签: android heroku django-rest-framework

我正在尝试从我开发的Android应用程序上传图像文件到服务器,但服务器的响应是以下html代码:

<!DOCTYPE html>
<html>
<head>
<meta name=“viewport” content=“width=device-width,initial-scale=1”>
<meta charset=“utf-8”>
<title>Application error</title>
<style media=“screen”>
html,body,iframe{
margin:0;
padding:0;
}
html,body{height:100%;
overflow:hidden;}
iframe{width:100%;
height:100%:
border:0;
}
</style>
</head>
</body>
<iframe src=“//www.herokucdn.com/error-pages/application-error.html”>    </iframe>
</body>
</html>

以下代码是在android端编写的,用于上传:

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

RequestBody req = new MultipartBody.Builder()
                     .setType(MultipartBody.FORM)
                     .addFormDataPart("userId", "1")
                     .addFormDataPart("image",sourceFile.getName(), RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                     .addFormDataPart("landmark","xyx")
                     .addFormDataPart("location","234124351245sdfgsdfb")
                     .build();

Request request = new Request.Builder()
                     .url("https://cyc-new.herokuapp.com/uploads/img_upload/")
                     .post(req)
                     .header("Authorization",credentials)
                     .build();

1 个答案:

答案 0 :(得分:0)

尝试在请求构建器中添加一个标头,用于指定数据的内容类型。例如,

Request request = new Request.Builder()
                 .url("https://cyc-new.herokuapp.com/uploads/img_upload/")
                 .post(req)
                 .header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
                 .header("Authorization",credentials)
                 .build();