我如何 post base64 图像的长编码字符串使用改造到服务器?
how do i post Long encoded string of base64 image to server using retrofit?
我正在尝试 post 编码图像字符串以改进 post 方法...调试后我在调试从图库中选择的图像时得到了很长的字符串......之后提交我可以在调试器中看到图像的长字符串编码....在 postman 中,当我检查时我显示 profile_pic: " "
为 null ...
需要帮助
如果我使用这样的端点我不会崩溃:
@FormUrlEncoded
@POST("update")
fun useredit(
@Header("access_token") token: String,
@Field("first_name") first_name:String,
@Field("last_name") last_name:String,
@Field("email") email:String,
@Field("dob") dob:String,
@Field("phone_no") phone_no: String,
@Field("profile_pic") profile_pic:String
):Call<LoginResponse>
响应代码:
profile = findViewById<View>(R.id.profilepic) as ImageView
profile?.setOnClickListener(View.OnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(intent, IMAGE)
})
editsubmit.setOnClickListener {
val first_name = firstname.text.toString().trim()
val last_name = lastname.text.toString().trim()
val email = emailregister.text.toString().trim()
val phone = phoneno.text.toString().trim()
val profile =convertToString()!!
val token: String =
SharedPrefManager.getInstance(
applicationContext
).user.access_token.toString()
RetrofitClient.instance.useredit(token,first_name,last_name,email,edittext1.text.toString(),phone,profile)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
Log.d("kjsfgxhufb",response.body()?.status.toString())
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
}
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
val path: Uri? = data.data
try {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, path)
profile?.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
我在调试器上看到长字符串,但在 postman
上看不到
后来我试了这个-->
我的端点:
@Multipart
@POST("update")
fun useredit(
@Header("access_token") token: String,
@Part("first_name") first_name:String,
@Part("last_name") last_name:String,
@Part("email") email:String,
@Part("dob") dob:String,
@Part("phone_no") phone_no: String,
@Part ("profile_pic")profile_pic: MultipartBody.Part?
):Call<LoginResponse>
activity 响应代码:-
profile?.setOnClickListener(View.OnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(intent, IMAGE)
})
editsubmit.setOnClickListener {
val first_name = firstname.text.toString().trim()
val last_name = lastname.text.toString().trim()
val email = emailregister.text.toString().trim()
val phone = phoneno.text.toString().trim()
val profile =convertToString()!!
val token: String =
SharedPrefManager.getInstance(
applicationContext
).user.access_token.toString()
val requestFile: RequestBody =
RequestBody.create(MediaType.parse("image/jpeg"), profile)
val body: MultipartBody.Part =
MultipartBody.Part.createFormData("image", "image.jpg", requestFile)
RetrofitClient.instance.useredit(token,first_name,last_name,email,edittext1.text.toString(),phone,body)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
Log.d("kjsfgxhufb",response.body()?.status.toString())
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
}
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
val path: Uri? = data.data
try {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, path)
profile?.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
好吧,我从上面的代码中崩溃了 -->java.lang.IllegalArgumentException:使用 MultipartBody.Part 的 @Part 参数不得在注释中包含零件名称。 (参数 #7)
您能否尝试更改如下所示的内容,因为您发送的都是字符串,因此多部分文件并不理想。只需使用请求正文发送它让我们看看服务器如何响应。
@Multipart
@POST("update")
fun useredit(
@Header("access_token") token: String,
@PartMap Map<String, RequestBody> partMap
):Call<LoginResponse>
无论你打电话到哪里,都这样做。
// create a map of data to pass along
RequestBody first_name = RequestBody.create(MediaType.parse("text/plain"),"your name here");
RequestBody last_name = RequestBody.create(MediaType.parse("text/plain"),"your last name here");
RequestBody email = RequestBody.create(MediaType.parse("text/plain"),"your email here");
RequestBody dob = RequestBody.create(MediaType.parse("text/plain"),"your dob here");
RequestBody phone_no = RequestBody.create(MediaType.parse("text/plain"),"your phone no here");
RequestBody profile_pic = RequestBody.create(MediaType.parse("text/plain"),"your picture base64 string here");
Map<String, RequestBody> map = new HashMap<>();
map.put("first_name", first_name);
map.put("last_name", last_name);
map.put("email", email);
map.put("dob", dob);
map.put("phone_no", phone_no);
map.put("profile_pic", profile_pic);
然后传递给调用函数
RetrofitClient.instance.useredit(token, map)//some code follows
编辑:
还需要以正确的格式发送图像 base64,如
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ.........
或者用下面的方法转成base64编码
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat. JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
将您的地图从 java 更改为 kotlin 实现,如下面的代码所示
val map: MutableMap<String, RequestBody> = HashMap()
map["first_name"] = first_name
map["last_name"] = last_name
map["email"] = email
map["dob"] = dob
map["phone_no"] = phone_no
map["profile_pic"] = profile_pic
我正在尝试 post 编码图像字符串以改进 post 方法...调试后我在调试从图库中选择的图像时得到了很长的字符串......之后提交我可以在调试器中看到图像的长字符串编码....在 postman 中,当我检查时我显示 profile_pic: " "
为 null ...
需要帮助
如果我使用这样的端点我不会崩溃:
@FormUrlEncoded
@POST("update")
fun useredit(
@Header("access_token") token: String,
@Field("first_name") first_name:String,
@Field("last_name") last_name:String,
@Field("email") email:String,
@Field("dob") dob:String,
@Field("phone_no") phone_no: String,
@Field("profile_pic") profile_pic:String
):Call<LoginResponse>
响应代码:
profile = findViewById<View>(R.id.profilepic) as ImageView
profile?.setOnClickListener(View.OnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(intent, IMAGE)
})
editsubmit.setOnClickListener {
val first_name = firstname.text.toString().trim()
val last_name = lastname.text.toString().trim()
val email = emailregister.text.toString().trim()
val phone = phoneno.text.toString().trim()
val profile =convertToString()!!
val token: String =
SharedPrefManager.getInstance(
applicationContext
).user.access_token.toString()
RetrofitClient.instance.useredit(token,first_name,last_name,email,edittext1.text.toString(),phone,profile)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
Log.d("kjsfgxhufb",response.body()?.status.toString())
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
}
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
val path: Uri? = data.data
try {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, path)
profile?.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
我在调试器上看到长字符串,但在 postman
上看不到后来我试了这个-->
我的端点:
@Multipart
@POST("update")
fun useredit(
@Header("access_token") token: String,
@Part("first_name") first_name:String,
@Part("last_name") last_name:String,
@Part("email") email:String,
@Part("dob") dob:String,
@Part("phone_no") phone_no: String,
@Part ("profile_pic")profile_pic: MultipartBody.Part?
):Call<LoginResponse>
activity 响应代码:-
profile?.setOnClickListener(View.OnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(intent, IMAGE)
})
editsubmit.setOnClickListener {
val first_name = firstname.text.toString().trim()
val last_name = lastname.text.toString().trim()
val email = emailregister.text.toString().trim()
val phone = phoneno.text.toString().trim()
val profile =convertToString()!!
val token: String =
SharedPrefManager.getInstance(
applicationContext
).user.access_token.toString()
val requestFile: RequestBody =
RequestBody.create(MediaType.parse("image/jpeg"), profile)
val body: MultipartBody.Part =
MultipartBody.Part.createFormData("image", "image.jpg", requestFile)
RetrofitClient.instance.useredit(token,first_name,last_name,email,edittext1.text.toString(),phone,body)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
Log.d("kjsfgxhufb",response.body()?.status.toString())
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
}
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
val path: Uri? = data.data
try {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, path)
profile?.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
好吧,我从上面的代码中崩溃了 -->java.lang.IllegalArgumentException:使用 MultipartBody.Part 的 @Part 参数不得在注释中包含零件名称。 (参数 #7)
您能否尝试更改如下所示的内容,因为您发送的都是字符串,因此多部分文件并不理想。只需使用请求正文发送它让我们看看服务器如何响应。
@Multipart
@POST("update")
fun useredit(
@Header("access_token") token: String,
@PartMap Map<String, RequestBody> partMap
):Call<LoginResponse>
无论你打电话到哪里,都这样做。
// create a map of data to pass along
RequestBody first_name = RequestBody.create(MediaType.parse("text/plain"),"your name here");
RequestBody last_name = RequestBody.create(MediaType.parse("text/plain"),"your last name here");
RequestBody email = RequestBody.create(MediaType.parse("text/plain"),"your email here");
RequestBody dob = RequestBody.create(MediaType.parse("text/plain"),"your dob here");
RequestBody phone_no = RequestBody.create(MediaType.parse("text/plain"),"your phone no here");
RequestBody profile_pic = RequestBody.create(MediaType.parse("text/plain"),"your picture base64 string here");
Map<String, RequestBody> map = new HashMap<>();
map.put("first_name", first_name);
map.put("last_name", last_name);
map.put("email", email);
map.put("dob", dob);
map.put("phone_no", phone_no);
map.put("profile_pic", profile_pic);
然后传递给调用函数
RetrofitClient.instance.useredit(token, map)//some code follows
编辑:
还需要以正确的格式发送图像 base64,如
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ.........
或者用下面的方法转成base64编码
private fun convertToString(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap?.compress(Bitmap.CompressFormat. JPEG, 100, byteArrayOutputStream)
val imgByte: ByteArray = byteArrayOutputStream.toByteArray()
return android.util.Base64.encodeToString(imgByte, android.util.Base64.NO_WRAP )
}
将您的地图从 java 更改为 kotlin 实现,如下面的代码所示
val map: MutableMap<String, RequestBody> = HashMap()
map["first_name"] = first_name
map["last_name"] = last_name
map["email"] = email
map["dob"] = dob
map["phone_no"] = phone_no
map["profile_pic"] = profile_pic