[英]onResponse() not called in REST call for Android using retrofit2
[英]ErrorCode: onResponse: 401 in retrofit android
我正在尝试使用 retrofit2 发出 POST 请求,但出现错误代码:401。我没有在 web 上找到可以帮助我解决问题的正确解决方案。
POST 请求的数据加载
InsertAcitvity.java
private void insertData() {
String name = mName.getText().toString().trim();
String email = mEmail.getText().toString().trim();
String phone = mPhone.getText().toString().trim();
String full_address = mFull_address.getText().toString().trim();
String name_of_university = mName_of_university.getText().toString().trim();
int graduation_year = Integer.parseInt(mGraduation_year.getText().toString().trim());
double cgpa = Double.parseDouble(mCgpa.getText().toString().trim()) ;
String experience_in_months = mExperience_in_months.getText().toString().trim();
String current_work_place_name = mCurrent_work_place_name.getText().toString().trim();
String applying_in = mApplying_in.getText().toString().trim();
int expected_salary = Integer.parseInt(mExpected_salary.getText().toString().trim()) ;
String reference = mFeference.getText().toString().trim();
String github_project_url = mGithub_project_url.getText().toString().trim();
long creationTime= System.currentTimeMillis();
long updateTime= System.currentTimeMillis();
//String token = LoginActivity.token;
Intent intent = getIntent();
// declare token as member variable
String token = intent.getStringExtra("TOKEN_STRING");
String CvUniqueId = UUID.randomUUID().toString();
String tsync_id = UUID.randomUUID().toString();
cvFileObject = new CvFileObject(CvUniqueId);
mainObject = new MainObjectClass(tsync_id, name, email, phone, full_address, name_of_university, graduation_year, cgpa, experience_in_months,
current_work_place_name, applying_in, expected_salary,field_buzz_reference, github_project_url, cvFileObject, creationTime, updateTime);
ClientMethod clientMethod = ApiClient.getClient().create(ClientMethod.class);
Call<MainResponseObjectClass> call = clientMethod.getValue(token,mainObject);
call.enqueue(new Callback<MainResponseObjectClass>() {
@Override
public void onResponse(@NonNull Call<MainResponseObjectClass> call, @NonNull Response<MainResponseObjectClass> response) {
if (response.isSuccessful()){
Toast.makeText(InsertActivity.this, response.body().getTsync_id(), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(InsertActivity.this, "access denied:(",Toast.LENGTH_LONG).show();
Log.d("ErrorCode", "onResponse: " + response.code());
}
}
@Override
public void onFailure(@NonNull Call<MainResponseObjectClass> call, @NonNull Throwable t) {
Log.d("Error", "onFailure: " + t.getMessage());
}
});
}
ClientMethod.java
public interface ClientMethod {
@POST("api/login/")
Call<Token>login(@Body Login login);
@POST("/api/v0/recruiting-entities/")
Call<MainResponseObjectClass> getValue(@Header("Authorization") String AutToken, @Body MainObjectClass mainObjectClass);
}
ApliClient.java 公共 class ApiClient {
public static final String BASE_URL = "https://myapi.test.com";
private static Retrofit retrofit = null;
public static Retrofit getClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
POST 请求代码
CvFileObject.java
public class CvFileObject {
@SerializedName("tsync_id")
private String tsync_id;
public CvFileObject(String tsync_id) {
this.tsync_id = tsync_id;
}
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
}
MainObjectClass.java
public class MainObjectClass {
@SerializedName("tsync_id")
private String tsync_id;
@SerializedName("name")
private String name;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("full_address")
private String full_address;
@SerializedName("name_of_university")
private String name_of_university;
@SerializedName("graduation_year")
private int graduation_year;
@SerializedName("cgpa")
private double cgpa;
@SerializedName("experience_in_months")
private String experience_in_months;
@SerializedName("current_work_place_name")
private String current_work_place_name;
@SerializedName("applying_in")
private String applying_in;
@SerializedName("expected_salary")
private int expected_salary;
@SerializedName("reference")
private String reference;
@SerializedName("github_project_url")
private String github_project_url;
@SerializedName("cv_file")
private CvFileObject fileObject;
@SerializedName("on_spot_update_time")
long on_spot_update_time;
@SerializedName("on_spot_creation_time")
long on_spot_creation_time;
公共MainObjectClass(字符串tsync_id,字符串名称,字符串email,字符串电话,字符串full_address,字符串name_of_university,int毕业年,双cgpa,字符串experience_in_months,字符串current_work_place_name,字符串applying_in,intexpected_salary,字符串参考,字符串github_project_url,CvFileObject文件对象,长时间, 长 on_spot_creation_time) { this.tsync_id = tsync_id; this.name = 名称; 这个.email = email; this.phone = 电话; this.full_address = full_address; this.name_of_university = name_of_university; this.graduation_year = 毕业年份; 这个.cgpa = cgpa; this.experience_in_months = experience_in_months; this.current_work_place_name = current_work_place_name; this.applying_in = 应用中; this.expected_salary = expected_salary; this.reference = 参考; this.github_project_url = github_project_url; this.fileObject = 文件对象; this.on_spot_update_time = on_spot_update_time; this.on_spot_creation_time = on_spot_creation_time; }
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFull_address() {
return full_address;
}
public void setFull_address(String full_address) {
this.full_address = full_address;
}
public String getName_of_university() {
return name_of_university;
}
public void setName_of_university(String name_of_university) {
this.name_of_university = name_of_university;
}
public int getGraduation_year() {
return graduation_year;
}
public void setGraduation_year(int graduation_year) {
this.graduation_year = graduation_year;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
public String getExperience_in_months() {
return experience_in_months;
}
public void setExperience_in_months(String experience_in_months) {
this.experience_in_months = experience_in_months;
}
public String getCurrent_work_place_name() {
return current_work_place_name;
}
public void setCurrent_work_place_name(String current_work_place_name) {
this.current_work_place_name = current_work_place_name;
}
public String getApplying_in() {
return applying_in;
}
public void setApplying_in(String applying_in) {
this.applying_in = applying_in;
}
public int getExpected_salary() {
return expected_salary;
}
public void setExpected_salary(int expected_salary) {
this.expected_salary = expected_salary;
}
public String reference() {
return reference;
}
public void setreference(String reference) {
this.reference = reference;
}
public String getGithub_project_url() {
return github_project_url;
}
public void setGithub_project_url(String github_project_url) {
this.github_project_url = github_project_url;
}
public CvFileObject getFileObject() {
return fileObject;
}
public void setFileObject(CvFileObject fileObject) {
this.fileObject = fileObject;
}
public long getOn_spot_update_time() {
return on_spot_update_time;
}
public void setOn_spot_update_time(long on_spot_update_time) {
this.on_spot_update_time = on_spot_update_time;
}
public long getOn_spot_creation_time() {
return on_spot_creation_time;
}
public void setOn_spot_creation_time(long on_spot_creation_time) {
this.on_spot_creation_time = on_spot_creation_time;
}
}
POST 请求响应代码
CvFileResponseObject.java
public class CvFileResponseObject {
@SerializedName("id")
int id;
@SerializedName("tsync_id")
private String tsync_id;
public CvFileResponseObject(String tsync_id) {
this.tsync_id = tsync_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTsync_id() {
return tsync_id;
}
public void setTsync_id(String tsync_id) {
this.tsync_id = tsync_id;
}
}
MainResponseObjectClass.java
公共 class MainResponseObjectClass {
@SerializedName("tsync_id")
private int tsync_id;
@SerializedName("name")
private String name;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("full_address")
private String full_address;
@SerializedName("name_of_university")
private String name_of_university;
@SerializedName("graduation_year")
private int graduation_year;
@SerializedName("cgpa")
private double cgpa;
@SerializedName("experience_in_months")
private String experience_in_months;
@SerializedName("current_work_place_name")
private String current_work_place_name;
@SerializedName("applying_in")
private String applying_in;
@SerializedName("expected_salary")
private String expected_salary;
@SerializedName("reference")
private String reference;
@SerializedName("github_project_url")
private String github_project_url;
@SerializedName("cv_file")
private CvFileResponseObject cvFileResponseObject;
@SerializedName("on_spot_update_time")
long on_spot_update_time;
@SerializedName("on_spot_creation_time")
long on_spot_creation_time;
@SerializedName("success")
private boolean success;
@SerializedName("message")
private String message;
public MainResponseObjectClass(int tsync_id, String name, String email, String phone,
String full_address, String name_of_university, int graduation_year,
double cgpa, String experience_in_months, String current_work_place_name,
String applying_in, String expected_salary, String reference,
String github_project_url, CvFileResponseObject cvFileResponseObject, long on_spot_update_time,
long on_spot_creation_time, boolean success, String message) {
this.tsync_id = tsync_id;
this.name = name;
this.email = email;
this.phone = phone;
this.full_address = full_address;
this.name_of_university = name_of_university;
this.graduation_year = graduation_year;
this.cgpa = cgpa;
this.experience_in_months = experience_in_months;
this.current_work_place_name = current_work_place_name;
this.applying_in = applying_in;
this.expected_salary = expected_salary;
this.reference = reference;
this.github_project_url = github_project_url;
this.cvFileResponseObject = cvFileResponseObject;
this.on_spot_update_time = on_spot_update_time;
this.on_spot_creation_time = on_spot_creation_time;
this.success = success;
this.message = message;
}
public int getTsync_id() {
return tsync_id;
}
public void setTsync_id(int tsync_id) {
this.tsync_id = tsync_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFull_address() {
return full_address;
}
public void setFull_address(String full_address) {
this.full_address = full_address;
}
public String getName_of_university() {
return name_of_university;
}
public void setName_of_university(String name_of_university) {
this.name_of_university = name_of_university;
}
public int getGraduation_year() {
return graduation_year;
}
public void setGraduation_year(int graduation_year) {
this.graduation_year = graduation_year;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
public String getExperience_in_months() {
return experience_in_months;
}
public void setExperience_in_months(String experience_in_months) {
this.experience_in_months = experience_in_months;
}
public String getCurrent_work_place_name() {
return current_work_place_name;
}
public void setCurrent_work_place_name(String current_work_place_name) {
this.current_work_place_name = current_work_place_name;
}
public String getApplying_in() {
return applying_in;
}
public void setApplying_in(String applying_in) {
this.applying_in = applying_in;
}
public String getExpected_salary() {
return expected_salary;
}
public void setExpected_salary(String expected_salary) {
this.expected_salary = expected_salary;
}
public String getField_buzz_reference() {
return field_buzz_reference;
}
public void setFeference(String reference) {
this.field_buzz_reference = reference;
}
public String getGithub_project_url() {
return github_project_url;
}
public void setGithub_project_url(String github_project_url) {
this.github_project_url = github_project_url;
}
public CvFileResponseObject getCvFileResponseObject() {
return cvFileResponseObject;
}
public void setCvFileResponseObject(CvFileResponseObject cvFileResponseObject) {
this.cvFileResponseObject = cvFileResponseObject;
}
public long getOn_spot_update_time() {
return on_spot_update_time;
}
public void setOn_spot_update_time(long on_spot_update_time) {
this.on_spot_update_time = on_spot_update_time;
}
public long getOn_spot_creation_time() {
return on_spot_creation_time;
}
public void setOn_spot_creation_time(long on_spot_creation_time) {
this.on_spot_creation_time = on_spot_creation_time;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
在这里我可以看到两个 API。 但我不确定您获得的是哪个 API 401。我可以帮助您了解您可以检查的可能性,请随时询问。
我有一个建议——你可以在拦截器中使用 header 和 retrofit 设置本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.