Android 如何使用改型将base64映像的长编码字符串发布到服务器?
我正在尝试发布图像的编码字符串以改进post方法…调试后,我在从库中选择的图像调试上获得了很长的字符串…提交后,我可以在调试器中看到图像的编码长字符串…在邮递员中,当我选中时,我显示Android 如何使用改型将base64映像的长编码字符串发布到服务器?,android,kotlin,retrofit,retrofit2,tobase64string,Android,Kotlin,Retrofit,Retrofit2,Tobase64string,我正在尝试发布图像的编码字符串以改进post方法…调试后,我在从库中选择的图像调试上获得了很长的字符串…提交后,我可以在调试器中看到图像的编码长字符串…在邮递员中,当我选中时,我显示配置文件\u pic:“为空…” 需要帮助吗 如果我像这样使用端点,我不会崩溃: @FormUrlEncoded @POST("update") fun useredit( @Header("access_token") token: String, @F
配置文件\u pic:“
为空…”
需要帮助吗
如果我像这样使用端点,我不会崩溃:
@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>
@FormUrlEncoded
@发布(“更新”)
趣味用户编辑(
@标头(“访问令牌”)令牌:字符串,
@字段(“名字”)名字:字符串,
@字段(“姓氏”)姓氏:字符串,
@字段(“电子邮件”)电子邮件:字符串,
@字段(“dob”)dob:字符串,
@字段(“电话号码”)电话号码:字符串,
@字段(“profile_pic”)profile_pic:字符串
):呼叫
响应代码:
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()
}
}
}
profile=findviewbyd(R.id.profilepic)作为ImageView
profile?.setOnClickListener(View.OnClickListener{
val intent=intent()
intent.type=“image/*”
intent.action=intent.action\u获取内容
startActivityForResult(意图、图像)
})
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标记:字符串=
SharedPrefManager.getInstance(
应用程序上下文
).user.access_token.toString()
修改client.instance.useredit(令牌、名字、姓氏、电子邮件、edittext1.text.toString()、电话、个人资料)
.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“res”和“+t”)
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
var res=响应
Log.d(“响应检查”,“”+response.body()?.status.toString())
if(res.body()?.status==200){
Toast.makeText(
应用上下文,
res.body()?.消息,
吐司,长度
).show()
Log.d(“kjsfgxhuff”,response.body()?.status.toString())
}
其他的
{
试一试{
瓦尔·乔布杰罗=
JSONObject(response.errorBody()!!.string())
Toast.makeText(
应用上下文,
jObjError.getString(“消息”)+jObjError.getString(“用户消息”),
吐司,长度
).show()
}捕获(e:例外){
Toast.makeText(applicationContext,e.message,Toast.LENGTH\u LONG.show())
Log.e(“errorr”,e.message)
}
}
}
})
}
}
private fun convertToString():字符串?{
val byteArrayOutputStream=byteArrayOutputStream()
位图?.compress(bitmap.CompressFormat.JPEG,100,byteArrayOutputStream)
val imgByte:ByteArray=byteArrayOutputStream.toByteArray()
返回android.util.Base64.encodeToString(imgByte,android.util.Base64.NO_WRAP)
}
在ActivityResult上覆盖乐趣(
请求代码:Int,
结果代码:Int,
数据:意图?
) {
super.onActivityResult(请求代码、结果代码、数据)
if(requestCode==IMAGE&&resultCode==Activity.RESULT\u OK&&data!=null){
val路径:Uri?=data.data
试一试{
位图=MediaStore.Images.Media.getBitmap(contentResolver,路径)
配置文件?.setImageBitmap(位图)
}捕获(e:IOException){
e、 printStackTrace()
}
}
}
我在调试器上看到长字符串,但在邮递员上看不到
后来我尝试了这个-->
我的端点:
@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>
@Multipart
@发布(“更新”)
趣味用户编辑(
@标头(“访问令牌”)令牌:字符串,
@部分(“名字”)名字:字符串,
@部分(“姓氏”)姓氏:字符串,
@部分(“电子邮件”)电子邮件:字符串,
@部分(“dob”)dob:字符串,
@部分(“电话号码”)电话号码:字符串,
@部件(“外形图”)外形图:多部件主体。部件?
):呼叫
活动响应代码:-
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()
}
}
}
profile?.setOnClickListener(View.OnClickListener{
val intent=intent()
intent.type=“image/*”
intent.action=intent.action\u获取内容
startActivityForResult(意图、图像)
})
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标记:字符串=
SharedPrefManager.getInstance(
应用程序上下文
).user.access_token.toString()
val requestFile:RequestBody=
create(MediaType.parse(“image/jpeg”),配置文件)
val body:MultipartBody.Part=
MultipartBody.Part.createFormData(“image”、“image.jpg”、requestFile)
修改client.instance.useredit(令牌、名字、姓氏、电子邮件、edittext1.text.toString()、电话、正文)
.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“res”和“+t”)
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
var res=响应
Log.d(“响应检查”,“”+response.body()?。
@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
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ.........
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 )
}
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