【问题标题】:VolleyError Unexpected response code 406 with REST GET requestVolleyError 带有 REST GET 请求的意外响应代码 406
【发布时间】:2021-03-02 23:42:11
【问题描述】:

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

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/

在此活动之前,我有一个 LoginActivity,它执行类似的请求,这是令牌在 GET 请求中的来源。它发送凭据并接收到 Auth-Token。这个可行,所以我不确定为什么之前的 sn-p 失败了。

登录请求:

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 http 错误代码保留为“不可接受”。这意味着您的请求的标头不正确
  • 这正是我所需要的!我将编辑我的问题以显示我是如何解决它的。

标签: java android rest get android-volley


【解决方案1】:

线索在请求响应错误代码 (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;
}

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

【讨论】:

    【解决方案2】:

    406 http 错误代码保留为“不可接受”。这意味着您的请求的标头不正确。我相信JsonObjectRequest 不管理标头,而是管理请求正文。 为了处理请求标头,您需要覆盖 VolleyRequest 类的 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() 是必要的。我会接受你的回答。感谢您的帮助。
    • 我很高兴,随时?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多