Android http Request / Response ContentType

客户端在进行http请求服务器的时候,需要告诉服务器请求的类型,服务器在返回给客户端的数据的时候,也需要告诉客户端返回数据的类型。

这个类型就是  ContentType  ,不同的ContentType 会影响客户端/服务器所看到的效果。contentType: 告诉服务器,我要发什么类型的数据

 

 1、默认的ContentType为  text/html 也就是网页格式. 

  •     text/plain :纯文本格式      
  •     text/xml :  XML格式
  •     image/gif :gif图片格式    
  •     image/jpeg :jpg图片格式 
  •     image/png:png图片格式

以application开头的媒体格式类型:

  •    application/xhtml+xml :XHTML格式
  •    application/xml     : XML数据格式
  •    application/atom+xml  :Atom XML聚合格式    
  •    application/json    : JSON数据格式
  •    application/pdf       :pdf格式  
  •    application/msword  : Word文档格式
  •    application/octet-stream : 二进制流数据(如常见的文件下载)
  •    application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

   另外一种常见的媒体格式是上传文件之时使用的:

  •     multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式

     以上就是我们在日常的开发中,经常会用到的若干content-type的内容格式。

 

比如:

 URL realUrl = new URL(url);
            //打开和URL之间的连接
            conn =  realUrl.openConnection();
            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "application/json");

  

 

private fun sendHttpRequest(json:JSONObject) { val urlString = "https://183.191.209.32:18802/app-card/writeCard/sdkDeviceRegist" //20250317 //val client = OkHttpClient() val client = createUnsafeOkHttpClient() val request = Request.Builder() .url(urlString) .post(json.toString().toRequestBody("application/json".toMediaType())) .build() lifecycleScope.launch(Dispatchers.IO) { try { client.newCall(request).execute().use { response -> // 处理响应(需在UI线程更新) if (!response.isSuccessful) throw IOException("Request failed: ${response.code}") //response.body?.use { body -> //val contentType = body.contentType()?.toString() ?: "unknown" //val contentLength = body.contentLength() //println("Received $contentLength bytes ($contentType)") //println(body.string()) //} // 解析代码 val gson = Gson() response.body?.let { responseBody -> try { val result = gson.fromJson(responseBody.charStream(), ApiResponse::class.java) //println("retCode: ${result.retCode}, retDesc: ${result.retDesc}") // 方法1:findViewById(基础方式) //val etRetCode = findViewById<EditText>(R.id.editText2) //etRetCode.setText(result.retCode) //val tvRetDesc = findViewById<TextView>(R.id.label2) //tvRetDesc.text = result.retDesc // 方法2:View Binding(推荐) withContext(Dispatchers.Main) { binding.editText2.setText(result.retCode) binding.label2.text = result.retDesc // 更新 TextView } saveDataDirectly(result.retCode) } catch (e: JsonSyntaxException) { // 处理JSON格式错误 } } } } catch (e: IOException) { //println("Network error: ${e.message}") //Toast.makeText(this, "Network error:${e.message}", Toast.LENGTH_SHORT).show() withContext(Dispatchers.Main) { binding.editText2.setText(e.message) } } } } Unresolved reference: lifecycleScope Unresolved reference: binding
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值