使用curl命令的Android排球请求

时间:2016-09-22 13:12:45

标签: android curl android-volley

我想以这种格式将内容传递给网络服务器

curl -H "Content-Type:application/json" -X POST -d {"email":"someone@example.com","first_name":"FName","last_na‌​me":"Lname","passwor‌​d":"pass123"}' 192.xxx.xxx.xxx:1111/register_user 

这就是我需要将值传递给服务器的方法。谁能帮我。怎么可能使用截击? 提前致谢

1 个答案:

答案 0 :(得分:4)

是的,你可以这样做

 JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "192.xxx.xxx.xxx:1111/register_user", new JSONObject("{"email":"someone@example.com","first_name":"FName","last_na‌​me":"Lname","passwor‌​d":"pass123"}"),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
         //call successful
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) { //error occur 
                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Content-Type", "application/json");
            params.put("Accept", "application/json");
            params.put("Accept-Encoding", "utf-8");
            return params;
        }
    };
    jsonRequest.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    mRequestQueue.addToRequestQueue(jsonRequest);