我写了使用改造上传图像的部分。
private fun sendPhoto(fileName: String, file: File) {
var requestBody : RequestBody = RequestBody.create(MediaType.parse("image/*"),file)
var body : MultipartBody.Part = MultipartBody.Part.createFormData("uploaded_file", fileName, requestBody)
var gson : Gson = GsonBuilder()
.setLenient()
.create()
var retrofit = Retrofit.Builder()
.baseUrl("http://192.168.0.36:8000")
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
var addfaceService:AddfaceService = retrofit.create(AddfaceService::class.java)
addfaceService.requestAddface(body).enqueue(object: Callback<Addface> {
override fun onFailure(call: Call<Addface>, t: Throwable) {
Log.d("레트로핏 결과1", t.message)
}
override fun onResponse(call: Call<Addface>, response: Response<Addface>) {
if (response?.isSuccessful){
Log.d("레트로핏 결과2",""+response?.body().toString())
}else{
Toast.makeText(getApplicationContext(), "Some error occured...", Toast.LENGTH_LONG).show();
}
}
})
接口部分是这个
这就是django服务器所做的。
如果执行此操作,django输出将显示如下。
def app_app_addface(request):
print("신호는 받음")
if request.method == 'POST':
print("리퀘스트 로그" + str(request.body))
photo = request.POST.get()
print("photo = " +photo)
return JsonResponse({'code': '0000', 'msg': '사진받았습니다.'}, status=200)
请求正文超出设置。DATA_UPLOAD_MAX_MEMORY_SIZE。错误的请求:/ app_addface /
老实说,Android Studio编码似乎是错误的,但我不知道如何实现。
此外,从Android Studion接收到django后,我不知道应该使用什么功能将其放入django。
我的github地址是https://github.com/chea-young/OnOoN2。
我认为主要问题是如何在android studio中发送图像并在django中获得它。