

Retrofit 2.0 throwing & ldquo; IllegalArgumentException: The @Field paramete...
source link: https://www.codesd.com/item/retrofit-2-0-throwing-illegalargumentexception-the-field-parameters-can-only-be-used-with-form-encoding-how-to-make-a-correct-api-query-and-repair-it.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

My problem is that I don't know how to start using Retrofit 2.0 with received API - mentioned below...
Firstly, I need to username, password, fbID (optional), gmailID (optional), twitID (optional), gender, birthDate, location (not required - if long and lat has values), longitude (optional), latitude (optional), profileImage (optional).
When all parameters are good - receive status = true
. If not - receive status = false
and required parameters which are wrong (e.g. mail is already taken)
So I can receive status = true
or status = false
and array with max 5 parameters (username, email, password, gender, birthDate).
I tried this API Interface
:
public interface AuthRegisterUserApi {
@PUT()
Call<AuthRegisterUserModel> getStatus(
@Field("username") String username,
@Field("email") String email,
@Field("password") String password,
@Field("fbID") String fbID,
@Field("gmailID") String gmailID,
@Field("twitID") String twitID,
@Field("gender") String gender,
@Field("birthDate") String birthDate,
@Field("location") String location,
@Field("longitude") String longitude,
@Field("latitude") String latitude,
@Field("profileImage") String profileImage
);
class Factory {
private static AuthRegisterUserApi service;
public static AuthRegisterUserApi getInstance() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConstants.REGISTER_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(AuthRegisterUserApi.class);
return service;
}
}
}
with this API models (Pastebin.com)
and this code in Activity
:
AuthRegisterUserApi.Factory.getInstance()
.getStatus(
usernameEditText.getText().toString(),
emailEditText.getText().toString(),
passwordEditText.getText().toString(),
"", "", "",
(maleRadioButton.isChecked() ? "male" : "female"),
mYear + "-" + mMonth+1 + "-" + mDay,
(geoLocationToggleButton.isChecked() ? geoLocationEditText.getText().toString() : ""),
(!geoLocationToggleButton.isChecked() ? "50" : ""),
(!geoLocationToggleButton.isChecked() ? "50" : ""),
"")
.enqueue(new Callback<AuthRegisterUserModel>() {
@Override
public void onResponse(Call<AuthRegisterUserModel> call, Response<AuthRegisterUserModel> response) {
if(response.isSuccessful()) {
if (response.body().isStatus()) {
showToast(getApplicationContext(), "Registration ok.");
} else {
response.body().getInfo().getUsername();
}
}
}
@Override
public void onFailure(Call<AuthRegisterUserModel> call, Throwable t) {
}
});
I have error: java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1) for method AuthRegisterUserApi.getStatus
I tried to register user using Postman and it works when I used option Body
-> x-www-form-urlencoded
.
How can I create I register
query to this API? Change @Field
to something else? I have got this error always...
EDIT: Need to change API Interface
to this:
public interface AuthRegisterUserApi {
@FormUrlEncoded
@PUT("/api/register")
Call<AuthRegisterUserModel> getStatus(
@Field("username") String username,
@Field("email") String email,
@Field("password") String password,
@Field("fbID") String fbID,
@Field("gmailID") String gmailID,
@Field("twitID") String twitID,
@Field("gender") String gender,
@Field("birthDate") String birthDate,
@Field("location") String location,
@Field("longitude") String longitude,
@Field("latitude") String latitude,
@Field("profileImage") String profileImage
);
class Factory {
private static AuthRegisterUserApi service;
public static AuthRegisterUserApi getInstance() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(AuthRegisterUserApi.class);
return service;
}
}
}
BASE_URL = http://ip_address:8400
for me...
But still got error: Response.rawResponse = Response{protocol=http/1.1, code=400, message=Bad Request, url=http://ip_address:8400/api/register}
. Using Postman with the same data I received code=201 Created. Don't know why...
Your request is not encoded right, but are postman, so do change that :
@FormUrlEncoded
@PUT("/api/register")
Call<AuthRegisterUserModel> getStatus(
@Field("username") String username,
@Field("email") String email,
@Field("password") String password,
@Field("fbID") String fbID,
@Field("gmailID") String gmailID,
@Field("twitID") String twitID,
@Field("gender") String gender,
@Field("birthDate") String birthDate,
@Field("location") String location,
@Field("longitude") String longitude,
@Field("latitude") String latitude,
@Field("profileImage") String profileImage);
Tell me if it's ok.
Recommend
-
13
Hive启动报错Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D首页
-
35
Java.lang.IllegalArgumentException: Removing a detached instance com.test.User # 5 advertisements I have a java EE project using JPA (transact...
-
9
java.lang.IllegalArgumentException: Called attach on a child which is not detached: BaseViewHolder{a32d238 position=0 id=-1, oldPos=-1, pLpos:-1} 抬头仰望星空,是否能发现自己的渺小。 ...
-
5
Javascript & ldquo; Private & rdquo; Vs properties of the instance advertisements I'm doing some Javascript R&D and,...
-
10
C # & ldquo; Undefined method & rdquo; When adding a new method to a simulated interface advertisements I am curren...
-
4
Labeling & ldquo; to-do & rdquo; s and colorful text in Markdown advertisements I'm using the GitHub flavoured Mark...
-
21
Imap_search () unknown search criteria & ldquo; OR & rdquo; advertisements Trying to build a IMAP Search query to filter...
-
8
November 2nd, 2016 | 8 minute readNo Cause for Concern — RxJava and Retrofit Throwing a TantrumLast week, we found an interesting API design issue in the Throwable class of the JDK that led to bugs in RxJava and Retrofit....
-
5
i'm throwing ephemeron party & you're invited12 December 2022 2:02 PM (
-
5
Advancing Space-Based Gravitational Wave Astronomy: Rapid Detection and Parameter Estimation Using Normalizing FlowsThis website uses cookies to ensure you get the best experience on our website.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK