【发布时间】:2021-05-04 16:55:07
【问题描述】:
我正在开发一个注册 API 它将json输入如下/请求参数
{"httpMethod":"POST",
"firstname":"Ali",
"lastname":"Patel",
"email":"alipatel05@gmail.com",
"password":"12345678",
"country":"Canada",
"state":"Quebec",
"city":"Montreal",
"type":"Parent"}
但是当我从 android 应用程序调用 api 时,它给了我不好的 响应 error code 400 但在邮递员上工作得很好。
我的 API 客户端
public class APIClient {
private static Retrofit retrofit = null;
public static final String BASE_URL = "https://5r8ndtx9zc.execute-api.us-east-2.amazonaws.com/";
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
/*Gson gson = new GsonBuilder()
.setLenient()
.create();*/
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
我的 API 接口
public interface APIInterface {
@Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json;charset=utf-8",
"Cache-Control: no-cache"
})
@POST("vaccinesApi")
Call<ResponseBody> registerUser(@Body JSONObject locationPost);
}
这是我的 api 调用
private void registerUser() {
HashMap<String, String> newhashMap = new HashMap<>();
JSONObject hashMap = new JSONObject();
try {
hashMap.put("httpMethod","POST");
hashMap.put("firstname",mEditFirstName.getText().toString().trim());
hashMap.put("lastname",mEditLastName.getText().toString().trim());
hashMap.put("email",mEditEmail.getText().toString().trim());
hashMap.put("password",mEditPassword.getText().toString().trim());
hashMap.put("country","Canada");
hashMap.put("state","Quebec");
hashMap.put("city",mSelectedCity);
hashMap.put("type",mUserType);
} catch (JSONException e) {
e.printStackTrace();
}
Call<ResponseBody> call = apiInterface.registerUser(hashMap);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
progressDialog.hide();
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
Log.e("SignupFragment", jsonObject.toString());
if (response.code() == 200) {
Toast.makeText(RegistrationActivity.this, "Success",Toast.LENGTH_SHORT).show();
/*Intent intent = new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(intent);
finishAffinity();*/
} else {
Toast.makeText(RegistrationActivity.this, "Failed",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
progressDialog.hide();
call.cancel();
Toast.makeText(RegistrationActivity.this, "Failed",Toast.LENGTH_SHORT).show();
}
});
}
我是否需要更改我的界面,否则我的 api 调用中可能会出现一些错误?
感谢安万斯
【问题讨论】:
-
我在浏览器中打开你的网址,得到这个
{"message":"Internal Server Error"},这是500错误代码不是400,在你的浏览器中试试这个5r8ndtx9zc.execute-api.us-east-2.amazonaws.com/vaccinesApi -
@miladsalimi 这是因为您直接调用没有正文的 api {"httpMethod":"POST", "firstname":"Ali", "lastname":"Patel", "email":" alipatel05@gmail.com", "password":"12345678", "country":"Canada", "state":"Quebec", "city":"Montreal", "type":"Parent"} 这是 api body 在邮递员中调用它会起作用,但在 android 应用程序中不起作用
-
@PrajwalWaingankar 不,我正在使用原始数据请求,请检查我的问题。
-
@AliPatel 检查答案,看看它是否能解决您的问题
标签: android api retrofit retrofit2 okhttp