gpt4 book ai didi

android - 使用未到达服务器的多部分表单数据改造 POST 请求

转载 作者:行者123 更新时间:2023-11-29 23:31:20 26 4
gpt4 key购买 nike

我有一个 API,我必须在其中发送普通字段和图像。为了调用 API,我正在使用 Retrofit。为了发送多部分请求,我使用了 Multipart Body Builder 方法。下面是相同的代码。

val builder = MultipartBody.Builder()
builder.setType(MultipartBody.FORM)
var jsonArray = JSONArray()
for ((index, value) in lineItems.withIndex()) {
var jsonObject = JSONObject()

jsonObject.accumulate("Room_id", lineItems[index].roomId)
jsonObject.accumulate("jobTitle", lineItems[index].jobTitle)
jsonObject.accumulate("floorLevel", lineItems[index].floorLevel)
jsonObject.accumulate("jobTrade", lineItems[index].jobArea)
jsonObject.accumulate("jobWork", lineItems[index].jobWork)
jsonObject.accumulate("desc", lineItems[index].desc)
jsonObject.accumulate("isFixed", lineItems[index].isFixed)
jsonObject.accumulate("hourlyCost", lineItems[index].cost)
jsonObject.accumulate("hourlyTotal", lineItems[index].total)
jsonObject.accumulate("hourlyDuration", lineItems[index].duration)
jsonObject.accumulate("fixedCost", lineItems[index].fixedCost)
if (lineItems[index].lineItemId!="") {
jsonObject.accumulate("_id", lineItems[index].lineItemId)
}
jsonArray.put(jsonObject)
Log.d("json array",jsonArray.toString())

}

builder.addFormDataPart("lineHeight", jsonArray.toString())
for ((i, value) in lineItems.withIndex()) {
var imageList = ArrayList<String>()

if (lineItems[i].imageList!=null && lineItems[i].imageList!!.size>0) {
imageList = lineItems[i].imageList!!

for ((j, path) in imageList.withIndex()) {
if (!imageList[j].contains("http")) {
val file = File(path)
val requestFile =
RequestBody.create(MediaType.parse("image/"+
file.name.substring(file.name.indexOf(".")+1)), file)
val body = MultipartBody.Part.createFormData("photos" + i,
file.name, requestFile)
builder.addPart(body)
}
}
}
}

val requestBody = builder.build()

当我调用上述 API 时,我得到:

502 Bad Gateway
http://18.222.231.171/api/lineheight/5bb45c4485453079ebb14b15 (4637ms)
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 03 Oct 2018 10:31:23 GMT
Content-Type: text/html
Content-Length: 182
Connection: keep-alive
502 Bad Gateway

502 Bad Gateway



nginx/1.10.3 (Ubuntu)

PS:- 该 api 在 iOS 和 Postman 中运行良好

调用代码如下:

fun addLineItems(@Header("Authorization") token: String,
@Path("jobId") jobId: String,
@Body body: okhttp3.RequestBody): Call<Response>

我向网络服务人员确认,当我从应用程序调用此 API 时,他们在日志中什么也得不到,而当从 Postman 调用相同时,日志会显示。

最佳答案

这是我的尝试

这就是我准备多部分实体的方式。

File file = new File(currentFilePath);
if (file.exists()) {
String name = URLConnection.guessContentTypeFromName(file.getName());
RequestBody requestFile = RequestBody.create(MediaType.parse(name), file);
MultipartBody.Part multipart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
}

这是上传文件的方法

@Multipart
@POST("your url")
fun uploadFile(@Header("Authorization") token: String, @Path("jobId") jobId: String,
@Part file: MultipartBody.Part): Call<Response>

也许你应该在你的方法中添加@Multipart annotayion

关于android - 使用未到达服务器的多部分表单数据改造 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com