OkHttpUtils
.get()
.url(Constant.HTTP_URL + "login")
.addParams("username", mLoginAccount)
.addParams("password", mLoginPassword)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
handleData(response);
}
@Override
public String parseNetworkResponse(Response response, int id) throws IOException {
//获取session的操作,session放在cookie头,且取出后含有“;”,取出后为下面的 s (也就是jsesseionid)
Headers headers = response.headers();
Log.e(TAG, "header " + headers);
List<String> cookies = headers.values("Set-Cookie");
String session = cookies.get(0);
Log.e(TAG, "onResponse-size: " + cookies);
String s = session.substring(0, session.indexOf(";"));
Log.e(TAG, "session is :" + s);
return super.parseNetworkResponse(response, id);
}
});
使用时:
OkHttpUtils
.post()
.url(Constant.HTTP_URL + "query")
.addHeader("cookie", s))
.build()
.execute(new StringCallback() {
网友评论