Retrofit2:将令牌授权添加为Cookie标头

时间:2019-11-30 17:44:25

标签: android cookies http-headers retrofit okhttp

使用Retrofit进行简单的API调用即可获取数据。 Cookie标头将与请求一起发送以授权请求。 Cookie必须采用以下形式: 令牌:

但是,授权失败,因此在执行时引发以下异常-

java.net.SocketTimeoutException: failed to connect to <url>/xy.xyz.xyz.xy (port xyz) from /xy.x.xy.xy (port xx) after 10000ms`

找到一种发送Cookie标头的方法,以便服务器接受请求。

这是我已经尝试过的 添加一个OkHttpClient并向其添加所需的cookie标头。我尝试过的另一件事是在@Header类型的改造方法中添加一个附加参数。但是所有这些方法都存在相同的问题。而且该API也已在邮递员上进行了尝试和测试,并且运行良好。

这是代码,

MainActivity.java

public class MainActivity extends AppCompatActivity {

    StudentObjectClass studentObject = new StudentObjectClass();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://<>/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
        Call<StudentObjectClass> call = retrofitInterface.getAttendanceData(730); //,"token=***; path=/api/; domain=.<>;");

        call.enqueue(new Callback<StudentObjectClass>() {
            @Override
            public void onResponse(Call<StudentObjectClass> call, Response<StudentObjectClass> response) {
                if(!response.isSuccessful())
                {
                    Log.d("Tag","Server Error.");
                    return;
                }

                StudentObjectClass DataList = response.body();

                Log.e("Tag","The message is - "+ DataList.getMessage());

            }

            @Override
            public void onFailure(Call<StudentObjectClass> call, Throwable t) {
                System.out.println("Failed to fetch");
                Log.d("Tag",t.toString());             
            }
        });
    }
}

RetrofitInterface.java

public interface RetrofitInterface {

 @Headers("Cookie: token=****; path=/***/; domain=.<>")
    @GET("api/attendance?for=AllClassAttendance")
    Call<StudentObjectClass> getAttendanceData(@Query("student_id")int student_id);
}

StudentObjectClass.java

public class StudentObjectClass {

    public class Attendance{
        public String className;
        public int totalLectures;
        public int present;
        public int sick;
        public int absent;
        public int late;

        public int getAbsent() {
            return absent;
        }

        public int getPresent() {
            return present;
        }

        public int getSick() {
            return sick;
        }

        public int getLate() {
            return late;
        }

        public int getTotalLectures() {
            return totalLectures;
        }

        public String getClassName() {
            return className;
        }
    }

    public String message;

    public List<Attendance> attendance;

    public String getMessage() {
        return message;
    }

    public List<Attendance> getAttendance() {
        return attendance;
    }

}

此外,JSON数据的格式为

{
    "message": "success",
    "attendance": [
        {
            "className": "xxx",
            "totalLectures": x,
            "present": x,
            "sick": x,
            "absent": x,
            "late": x
        },
        {
            "className": "xx",
            "totalLectures": x,
            "present": x,
            "sick": x,
            "absent": x,
            "late": x
        }
    ]
}

0 个答案:

没有答案