所以我正在尝试发出服务器请求,此请求的预期正文如下所示:["1"]
但是如果我将字符串作为 "[\"1\"]"
发送,则会出现错误
查看正文,很明显我必须发送一个 JSONArray
,我尝试使用以下代码实现此目的:
JSONArray array = new JSONArray();
array.put("1");
但我在 Stetho 上的 body 显示为:
{
"values":["1"]
}
也许我使用了错误的类或其他东西!
编辑:
这是我的api调用
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
RetrofitService retrofitService2 =new Retrofit.Builder()
.baseUrl("http://blynk-cloud.com/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RetrofitService.class);
retrofitService2.closeDoor("3bf11f14e6094dd5a8f31f6d6fac8f3d",
"[\"1\"]")
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
{
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
关闭门方法:
@PUT("/{id}/update/D12")
Call<String> closeDoor(@Path("id") String id, @Body String code);
最佳答案
如果你想像这样发送请求 JSON ["1"]
更改为:
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("1"));
//output ["1"]
代替:
JSONArray array = new JSONArray();
array.put("1");
关于java - 在改造中发送仅包含字符串的 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47925982/