volleyerror对rest get请求的意外响应代码406

w8rqjzmb  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(498)

我正在尝试创建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中的截击请求中使用该请求,但是,我得到了回报: 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请求中令牌的来源。它发送凭据并接收身份验证令牌。这一个有效,所以我不确定为什么前面的代码段失败了。
登录请求:

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会失败。我觉得我的语法是正确的,但也许我遗漏了什么?

4ioopgfo

4ioopgfo1#

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;
}
qxgroojn

qxgroojn2#

线索在请求-响应错误代码(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() 不工作(这是在任何教程我看,所以我需要研究更多),这是什么修复它。我现在得到了成功的回应。

相关问题