gpt4 book ai didi

android - 在 Volley 中设置 cookie 不起作用

转载 作者:行者123 更新时间:2023-11-29 14:32:24 25 4
gpt4 key购买 nike

这是我使用的代码:

 if (toEditText.getText().toString().length() > 0) {
getName = toEditText.getText().toString().substring(0, toEditText.getText().toString().length() - 1) + "";
StringBuffer sb = new StringBuffer();
sb.append(Constants.SERVER_IP);
sb.append("/api/groups.json");
String url = sb.toString();
LogService.log(TAG, "download url: " + url);
JSONObject juser = new JSONObject();
JSONArray userIds = new JSONArray();
for (int i = 0; i < user_ids.size(); i++) {
userIds.put(user_ids.get(i));
}

try {
juser.put("group_name", getName);
juser.put("owner_id", UserCredentialsPersistence.getUserId(context));
juser.put("user_ids", userIds);
juser.put("group_type", null);
juser.put("name_protected", true);
LogService.log(TAG, "JSON: " + juser.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, juser, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "%%%%%Response:" + response.toString());
int group_id = 0;
group_name = "";
if (response != null) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject userObj2 = (JSONObject) response.get("group");
group_id = userObj2.getInt("id");
group_name = userObj2.getString("name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
LogService.log(TAG, "id: " + group_id);
LogService.log(TAG, "name: " + group_name);
getName = "";
toEditText.setText("");
GroupAndConversationManager.getInstance(context).addNewConversation(group_id, group_name);

hideSearchFild();

int curr_group_id = UserCredentialsPersistence.getCurrentGroupId(context);

changeGroup(curr_group_id, group_id);
showGroupToast(group_name);
setVideoPlayback();

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, error.getMessage());
}
}) {
@Override
public HashMap<String, String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
StringBuilder builder = new StringBuilder();
builder.append(UserCredentialsPersistence.restoreCookie(context));
params.put("Cookie", builder.toString());

return params;
}
};
mRequestQueue.add(jr);
}

现在,Logcat 返回此行:

07-12 11:21:53.702: I/VideoPlayerFragment(27428): java.io.IOException: No authentication challenges found

我也曾尝试覆盖 getHeaders,而不是 getParams(),但出现错误,因此网络服务无法正常工作。这是我想到覆盖 getParams/getHeaders 的地方:How to set custom header in Volley Request谁能向我解释我做错了什么?

最佳答案

使用这个覆盖解决了它:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Cookie", UserCredentialsPersistence.restoreCookie(context).toString());
return headers;
}

关于android - 在 Volley 中设置 cookie 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17610685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com