1

如何使用 volly 为使用 aws api 网关创建的 post REST URL 从 android 发送正确的 http 请求?

如何从 android 发出正确的 http 请求,以便 api 网关接受这些值。

Api 网关内部测试工作正常。这是请求中需要传递的参数。

   

{
  "id": "22",
  "res": "10",
  "rate": "75",
  "index": "1"
}

//this method is used call http request


public static void newPost2(Context context){
        String awsAccessKey = "awsAccessKey";
        final String awsSecretKey = "awsSecretKey";

        String dateStr = ClientSSLSocketFactory.getServerTime();
        String scope =  dateStr + "/us-east-1/execute-api/aws4_request,";
        String headerNames = "host;x-amz-date";

        final String credentialsAuthorizationHeader = "Credential=" + awsAccessKey + "/" + scope;
        String signedHeadersAuthorizationHeader = "SignedHeaders=" + headerNames;
        String signatureAuthorizationHeader = "Signature=" + "hex encoded signature";
        final String authorization = "AWS4-HMAC-SHA256 "
                + credentialsAuthorizationHeader + ", "
                + signedHeadersAuthorizationHeader + ", "
                + signatureAuthorizationHeader;


        final String url="https://xxxxxxx2.execute-api.us-east-1.amazoxxxxxxxxxxxxxxxx";

        RequestQueue queue = Volley.newRequestQueue(context);
        StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //      mPostCommentResponse.requestCompleted();
                System.out.print("\n\n\n......post ok."+response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //  mPostCommentResponse.requestEndedWithError(error);
                System.out.print("\n\n\n.......post error."+error);
                error.printStackTrace();

            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                //  params.put("Content-Type", "application/json; charset=utf-8");
                params.put("id","22");
                params.put("rate","10");
                params.put("res", "75");
                params.put("index","1");

                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                // params.put("Content-Type","application/x-www-form-urlencoded");

                params.put("Host",  "apigateway.us-east-1.amazonaws.com");
                params.put("x-amz-date", ClientSSLSocketFactory.getawsnettime());
                params.put("Authorization", authorization);
                params.put("Content-Type", "application/x-amz-json-1.0");

                return params;
            }
        };
        queue.add(sr);
    }

但在日志中显示错误

...stringr..header......post error.com.android.volley.AuthFailureError
11-30 14:52:14.140 4663-4663/com.example.acernrc.noveleven W/System.err: com.android.volley.ServerError
11-30 14:52:14.140 4663-4663/com.example.acernrc.noveleven W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)

4

0 回答 0