我已經實現了我的Android代碼第一次改造和麪向follwing問題如何發送的JSONObject到改造API調用的Android
收到以下錯誤: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(){...});'。 –