Java 截击错误REST GET请求的意外响应代码406

Java 截击错误REST GET请求的意外响应代码406,java,android,rest,get,android-volley,Java,Android,Rest,Get,Android Volley,我正在尝试创建一个curl-GET请求的Android版本。在终端上运行良好,但在应用程序中失败 curl请求(URL不是真实的): 它会毫无问题地返回我需要的东西 好吧,现在我想在Android的截击请求中使用该请求,但是,我回来了: 的意外响应代码406https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/ 我尝试请求的代码如下(将显示预先设置的相关变量): 在此活动之前,我有

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

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

它会毫无问题地返回我需要的东西

好吧,现在我想在Android的截击请求中使用该请求,但是,我回来了:

的意外响应代码406https://api.myendpoint.com/REST/GSLB/zone-to-get-records-from.com/fqdn-of-domain.com/

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

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

登录请求:

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();
           }

    }
JSONObject object=new JSONObject();
试一试{
put(“user_name”,usernameEditText.getText().toString());
put(“customer_name”,customernameEditText.getText().toString());
put(“password”,passwordEditText.getText().toString());
put(“内容类型”、“应用程序/json”);
}捕获(JSONException e){
Log.d(标记,例如getMessage());
}
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,Request,object,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
if(响应!=null){
Log.d(“JSON”,String.valueOf(response));
字符串标记=null;
pDialog.disclose();
试一试{
token=response.getJSONObject(“数据”).getString(“token”);
Log.d(“JSON”,令牌);
}捕获(JSONException e){
Toast.makeText(LoginActivity.this,“+e.getMessage(),Toast.LENGTH_LONG.show();
e、 printStackTrace();
}
Intent loginIntent=newintent(LoginActivity.this,SelectActivity.class);
loginIntent.putExtra(“令牌”,令牌);
LoginActivity.this.startActivity(LoginEnt);
}否则{
Toast nullToast=Toast.makeText(LoginActivity.this,“无效凭据\n请重试”,Toast.LENGTH\u LONG);
nullToast.show();
usernameEditText.getText().clear();
CustomerNameDitText.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()
方法发送我的标题,如下所示:

@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;
}
@覆盖
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
参数put(“身份验证令牌”,令牌);
参数put(“细节”、“Y”);
返回参数;
}

虽然我不明白为什么我的
对象.put()
不起作用(这在我看过的任何教程中都有,所以我需要进一步研究),但这就是修复它的原因。我现在成功地得到了响应。

406 http错误代码保留为“不可接受””。这意味着您的请求的标题不正确。我认为
JsonObjectRequest
不管理标题,而是管理请求正文。 为了处理请求头,您需要重写VolleyRequest类中的getHeaders()方法,如下所示:

@覆盖
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
参数put(“身份验证令牌”,令牌);
参数put(“细节”、“Y”);
返回参数;
}

406 http错误代码保留为“不可接受”。这意味着您的请求的标题不正确,这正是我所需要的!我将编辑我的问题,以说明我是如何解决的。您解释了为什么需要getHeaders()。我将接受您的回答。感谢您的帮助。我很乐意随时提供帮助
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();
           }

    }
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());
   }
@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;
}