带有 REST GET 请求的 VolleyError 意外响应代码 406

VolleyError Unexpected response code 406 with REST GET request

我正在尝试创建 Android 版本的 curl GET 请求。在终端运行良好,但在应用程序中运行失败。

curl 请求(url 不是真实的):

curl -d '{"detail": "Y"}' -H "Content-Type:application/json" -X GET -H "Auth-Token:longstringhereforauthtoken" https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/

它毫无问题地回馈我所需要的。

嗯,现在我想在 Android 的 Volley 请求中使用该请求,但是,我回来了:

Unexpected response code 406 for https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/

我尝试请求的代码如下(将显示预先设置的相关变量):

private String url;
private String request;
private String searchRequest;

String selected = getIntent().getStringExtra("node");
String token = getIntent().getStringExtra("token");

url = getResources().getString(R.string.api_url);
request = url + "/Session/";
searchRequest = url + "/GSLB/"+selected+"/"+selected+"/";


ProgressDialog searchDialog = new ProgressDialog(MainActivity.this);
                        searchDialog.setMessage("Getting zone info...");
                        searchDialog.show();

                        JSONObject object = new JSONObject();
                        try {
                            object.put("Content-Type", "application/json");
                            object.put("Auth-Token", token);
                            object.put("detail", "Y");
                        } catch(JSONException e) {
                            Log.d(TAG, e.getMessage());
                        }
                        Log.d(TAG, "Selected "+icon+", Token: "+token);
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, searchRequest, object,
                                new Response.Listener<JSONObject>() {
                                    @Override
                                    public void onResponse(JSONObject response) {
                                        searchDialog.dismiss();
                                        Log.d(TAG, String.valueOf(response));
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                searchDialog.dismiss();
                                if( error instanceof NetworkError) {
                                    Log.d(TAG, error.toString());
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                } else if( error instanceof ServerError) {
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                    Log.d(TAG, error.toString());
                                } else if( error instanceof AuthFailureError) {
                                    Log.d(TAG, error.toString());
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                } else if( error instanceof ParseError) {
                                    Log.d(TAG, error.toString());
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                } else if( error instanceof NoConnectionError) {
                                    Log.d(TAG, error.toString());
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                } else if( error instanceof TimeoutError) {
                                    Log.d(TAG, error.toString());
                                    Log.d(TAG, "Request to "+searchRequest+" FAILED...");
                                }
                            }
                        });
                        RequestQueue searchRequestQueue = Volley.newRequestQueue(MainActivity.this);
                        searchRequestQueue.add(jsonObjectRequest);

那是我收到错误的时间:

BasicNetwork.performRequest: Unexpected response code 406 for https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/

在此之前 activity,我有一个执行类似请求的 LoginActivity,它是 GET 请求中令牌的来源。它发送凭据并接收 Auth-Token。这个有效,所以我不确定为什么前面的代码片段失败了。

登录请求:

JSONObject object = new JSONObject();
try {
    object.put("user_name", usernameEditText.getText().toString());
    object.put("customer_name", customernameEditText.getText().toString());
    object.put("password", passwordEditText.getText().toString());
    object.put("Content-Type", "application/json");
} catch(JSONException e) {
    Log.d(TAG, e.getMessage());
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, request, object,
  new Response.Listener<JSONObject>() {
      @Override
      public void onResponse(JSONObject response) {
         if(response != null) {
            Log.d("JSON", String.valueOf(response));
            String token = null;
            pDialog.dismiss();
            try {
               token = response.getJSONObject("data").getString("token");
               Log.d("JSON", token);
             } catch (JSONException e) {
                  Toast.makeText(LoginActivity.this, "" + e.getMessage(),Toast.LENGTH_LONG).show();
                  e.printStackTrace();
                                    }
                  Intent loginIntent = new Intent(LoginActivity.this, SelectActivity.class);
                  loginIntent.putExtra("token", token);
                  LoginActivity.this.startActivity(loginIntent);
         } else {
            Toast nullToast = Toast.makeText(LoginActivity.this, "Invalid Credentials\nPlease try again", Toast.LENGTH_LONG);
            nullToast.show();
            usernameEditText.getText().clear();
            customernameEditText.getText().clear();
            passwordEditText.getText().clear();
           }

    }

}

我已经在 PHP 和 cURL 中完成了这些请求,但似乎无法理解为什么 Android 会失败。我觉得我的语法是正确的,但也许我遗漏了什么?

线索在请求响应错误代码 (406) 中。而不是这个:

JSONObject object = new JSONObject();
try {
  object.put("Content-Type", "application/json");
  object.put("Auth-Token", token);
  object.put("detail", "Y");
 } catch(JSONException e) {
     Log.d(TAG, e.getMessage());
   }

我使用 getHeaders() 方法发送我的 headers,像这样:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
  Map<String, String>  params = new HashMap<String, String>();
  params.put("Content-Type", "application/json");
  params.put("Auth-Token", token);
  params.put("detail", "Y");

  return params;
}

虽然我不明白为什么我的 object.put() 不起作用(它出现在我查看的任何教程中,所以我需要进行更多研究),这就是修复它的方法。我现在已经成功收到回复了。

406 http 错误代码保留为“不可接受”。这意味着您的请求有一个不正确的 header。我相信 JsonObjectRequest 不管理 header,但请求 body。 为了根据请求 header 工作,您需要覆盖 VolleyRequest class 中的 getHeaders() 方法,例如:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
  Map<String, String>  params = new HashMap<String, String>();
  params.put("Content-Type", "application/json");
  params.put("Auth-Token", token);
  params.put("detail", "Y");

  return params;
}