这是我使用的代码:

 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(),但是出现错误,因此Web服务无法正常工作。这是从这里获得覆盖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;
}

10-06 02:02