1

这是我使用的代码:

 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的地方:如何在Volley Request中设置自定义标头 有人可以向我解释我做错了什么吗?

4

1 回答 1

8

使用此覆盖解决了它:

@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;
}
于 2013-07-16T07:26:19.020 回答