android - 如何使用 Retrofit 发布 (x-www-form-urlencoded) Json 数据?

标签 android json retrofit2

<分区>

当我在 body (x-www-form-urlencoded) 中发布数据时,它在 Postman 中工作正常。但是使用 Retrofit 2.0 Android 它不起作用。

@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("/api/jsonws/pushUserData")
Call<ResponseBody>  pushData(@Body JSONObject jsonObj);

JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("role","owner");
            jsonObject.put("id","27001");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiInterface apiInterface1= ApiClient.getClientAuthentication().create(ApiInterface.class);
        Call<ResponseBody> responseBodyCall = apiInterface1.pushData(jsonObject);

此代码无效。我也尝试@FormUrlEncoded。

最佳答案

尝试使用 @FormUrlEncoded 并使用 @Field 而不是 @Body

@FormUrlEncoded
@POST("/api/jsonws/pushUserData")
Call<ResponseBody>  pushData(@Field("role") String role, @Field("id") String id);

ApiInterface apiInterface1= ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<ResponseBody> responseBodyCall = apiInterface1.pushData("owner","27001");

关于android - 如何使用 Retrofit 发布 (x-www-form-urlencoded) Json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608396/

相关文章:

android - 与项目 'com.google.code.gson:gson' 中的依赖项 ':app' 冲突。应用程序 (2.7) 和测试应用程序 (2.8.0) 的已解决版本不同

Android Retrofit 2 简单 XML 转换器

android - 具有复杂Json结构的房间

java - 实现异步任务: An error occured while executing doInBackground() throws Runtime exception

javascript - 使用 vanilla JavaScript 将 JSON 数据存储在变量中

java - 在 Google Cloud End Points 中一次插入多个相同类型的记录

javascript - 如何向 json 添加新值

android - 没有足够的空间来展示广告!想要 : <480, 75>,有 : <454, 90>

java - 无法在分离 View 上启动此动画师!显露效果

android - 在 OSX 上为 Android Rom 创建缓存时出错