errorcode:onresponse:401

6uxekuva  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(216)

我正在尝试使用Reformation2发出post请求,但我得到错误代码:401。我在网上找不到合适的解决方法来帮助我解决问题。


post请求的数据加载
插入性.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());

            }
        });

    }

客户端方法.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);

}

java公共类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;
}

}
邮政请求代码
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;

public mainobjectclass(字符串tsync\u id,字符串name,字符串email,字符串phone,字符串full\u address,字符串name\u of university,字符串毕业年份,双cgpa,字符串月经验,字符串current\u work\u place\u name,字符串application\u in,int expected\u salary,字符串reference,字符串github\u project\u url,cvfileobject fileobject,long on \u spot \u update \u time,long on \u spot \u creation \u time){this.tsync \u id=tsync \u id;this.name=名称;this.email=电子邮件;this.phone=电话;this.full\u address=完整地址;this.name_of_university=大学名称;this.gradication\ u year=毕业\年;this.cgpa=cgpa;this.experience\u in \u months=月经验;this.current\u work\u place\u name=当前\u work\u place\u name;this.applying_in=正在应用\u in;this.expected_salary=期望_salary;this.reference=参考;this.github\u project\u url=github\u project\u url;this.fileobject=文件对象;this.on\u spot\u update\u time=现场更新时间;this.on \u spot \u creation \u time=现场\u creation \u 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;
}

}
请求后响应代码
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;
    }
}

mainResponseObject.java类
公共类MainResponseObject类{

@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;
}

}

hi3rlvi2

hi3rlvi21#

在这里我可以看到两个api。但我不确定你将得到哪个api。我可以帮助你的可能性,你可以检查,请随时问。
假设您得到401登录api-原因可能是您尝试使用错误的凭据。
假设您得到的是401forgetvalueapi,原因可能是您传递了从登录响应中得到的错误令牌。尤其是您需要用键和值交叉检查标题。
我有一个建议-你可以在拦截器中使用header,并对其进行改装。

相关问题