【问题标题】:How to send a JSONObject into a Retrofit API call Android如何将 JSONObject 发送到 Retrofit API 调用 Android
【发布时间】:2017-07-20 15:54:01
【问题描述】:

我已经在我的 Android 代码中实现了第一次 Retrofit,并且面临以下问题:

java.lang.IllegalArgumentException:@Body 参数不能与表单或多部分编码一起使用。 (参数#1)

我已经实现了如下代码:

public interface APIService {
    @FormUrlEncoded
    @POST("/")
    @Headers({
        "domecode: axys",
        "Content-Type: application/json;charset=UTF-8"
    })
    Call<JsonObject> sendLocation(@Body JsonObject jsonObject);
}


public class ApiUtils {

    static String tempUrl = "http://192.168.16.114:8092/api/v1/location/tsa/";
    public static APIService getAPIService() {

        return RetrofitClient.getClient(tempUrl).create(APIService.class);
    }
}

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if(retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

将值传递给 API 调用

JsonObject postParam = new JsonObject();
try {
    postParam.addProperty(Fields.ID, "asdf");
}

Call<JsonObject> call = apiService.sendLocation(postParam);
call.enqueue(
    new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            Log.d("response", "Getting response from server: " + response);
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            Log.d("response", "Getting response from server: " + t);
        }
    }
);

【问题讨论】:

  • 不给回调apiService.sendLocation(jsonObject.enqueue(new Callback&lt;JSONObject&gt;(){ ... });.

标签: android api retrofit retrofit2


【解决方案1】:

您正在使用 Android 内部 JSON API。您需要改用 Gson 的类。

Call<JsonObject> sendLocation(@Body JsonObject jsonObject);

因此导入声明

import com.google.gson.JsonObject;

另一个错误是将回调作为参数传递给请求

Call<JsonObject> call = apiService.sendLocation(jsonObject);
call.enqueue(new Callback<JsonObject>() {
   @Override
   public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
      Log.d("response", "Getting response from server: "+response);
   }

   @Override
   public void onFailure(Call<JsonObject> call, Throwable t) {
      Log.d("response", "Getting response from server: " + t);
   }
});

【讨论】:

  • java.lang.IllegalArgumentException:@Body 参数不能与表单或多部分编码一起使用。 (参数#1)我已经导入了import com.google.gson.JsonObject;并更改并交叉检查但得到相同的错误:
  • 你需要把JSONObject改成JsonObject
  • 请检查我更新的代码:JsonObject postParam = new JsonObject();尝试 { postParam.addProperty(Fields.ID, "asdf") postParam.addProperty(Fields._ID_OLD, "asdf"); postParam.addProperty(Fields.LATI, "asdf"); postParam.addProperty(Fields.LON, "asdf") postParam.addProperty(Fields.OR_IDS, "asdf"); } catch (JsonIOException e) { Log.d(TAG, e.getMessage()); }
  • 你不需要这个参数Callback&lt;JSONObject&gt; callback
  • 感谢您宝贵的时间,我已经按照您所说的更新了代码,又遇到了同样的问题,是不是因为 JsonObject postParam = new JsonObject();尝试 { postParam.addProperty(Fields.ID, "asdf"); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 2020-07-18
  • 2012-08-04
相关资源
最近更新 更多