无法使用Volley从Android发布数据到api

时间:2018-05-24 11:28:49

标签: android json android-volley

我想使用post方法注册用户。 这是API链接:

`http://125.62.194.181/SmartTrackerAPI/api/User/SignUp/UserName/EmpId/Email/Mobile/Password`

我是JSON的新手。我知道如何从JSON获取数据,但是当谈到POST方法时,我不知道怎么做?

需要帮助, 谢谢。

1 个答案:

答案 0 :(得分:0)

使用POST发送到服务器的数据存储在HTTP请求的请求主体中:

以下是使用Volley的示例: -

    String url = "YOUR URL";
    RequestQueue queue = Volley.newRequestQueue(this);//Creating The Request Queue
    StringRequest postRequest = new StringRequest(Request.Method.POST/*Setting The Method As Post*/, url/*Passing The Url Of Your API*/, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response", response);
           //Here You Will Get Your JSON as response.
            }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {
                 // error
                 Log.d("Error.Response", response);
             //Here You Will Get Your Error related to your Request.
           }
        }
    ) {     
        @Override
        protected Map<String, String> getParams() 
        {  
                Map<String, String>  params = new HashMap<String, String>();  
                params.put("name", "Alif");  
                params.put("domain", "http://itsalif.info");//Puting The  Parameters In Map To Pass It To The API.

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