如何将params值作为json数组发送

时间:2017-08-01 12:00:18

标签: android android-studio android-volley kotlin retrofit

我必须为注册api传递请求参数值,并且值应该传递给此文件。因为我得到凌空错误400.任何人都可以帮忙解决。

的Json

{
    "number":"12345678",
    "password":"12345678",
    "first_name":"abc",
    "last_name":"xyz",
    "email":"abc@gmail.com"
}

还必须在标题

中传递内容类型和身份验证
fun signup(requestp:String,number: String, password: String, firstname: String,lastname: String,email: String) {





    showDialog()
    var strReq: StringRequest
    val urlapireq= APIReqParam()
    val urlresult = URLCollection()
    var st:Boolean




        pDialog.setMessage("Signing...")
        showDialog()

        strReq= object : StringRequest(Request.Method.POST,
                URLCollection.obj.BASE_URL,object : Response.Listener<String> {

            override fun onResponse(response: String) {
                println("Change Password Response" + response.toString())
                hideDialog()
                try {

                    val responseObj = JSONObject(response)


                    // Parsing json object response
                    // response will be a json object

                    val status = responseObj.getString(urlapireq.STATUS)
                    val message = responseObj.getString(urlapireq.MESSAGE)
                    val success = responseObj.getString(urlapireq.SUCCESS)
                    val error = responseObj.getString(urlapireq.ERROR)
                    val responsecode = responseObj.getString(urlapireq.RESPONSECODE)
                    if (responsecode == "200") {
                       Toast.makeText(applicationContext,success,Toast.LENGTH_SHORT).show()

                    } else {
                        Toast.makeText(applicationContext, error, Toast.LENGTH_LONG).show()
                    }

                } catch (e: JSONException) {
                    println("Request for SMS error : " + e.message)
                    /*Boast.makeText(getApplicationContext(),
                           "Request for SMS error : " + e.getMessage(),
                           Toast.LENGTH_LONG).show();*/

                }


            }
        }, object : Response.ErrorListener {

            override fun onErrorResponse(error: VolleyError) {
                System.out.println("Change Password: " + error.message)

                /*Boast.makeText(getApplicationContext(),"Registration Error: "+
                   error.getMessage(), Toast.LENGTH_LONG).show();*/

            }
        }) {



            override fun getParams(): Map<String, String> {
                //Creating HashMap
                val params = HashMap<String, String>()
                params.put(urlapireq.REQUEST, "signup")
                params.put(urlapireq.Number, number)
                params.put(urlapireq.Password , password)
                params.put(urlapireq.FIRSTNAME , firstname)
                params.put(urlapireq.LASTNAME , lastname)
                params.put(urlapireq.EMAIL , email)
                return params
            }
            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val headers = HashMap<String, String>()
                headers.put("Content-Type", "application/json")

                return headers
            }
        }

    println("Verification Url : " + strReq!!)
    // val crashinst=CrashApplicationClass()
    CrashApplicationClass.instance?.addToRequestQueue(strReq,TAG)
}

但因为它给了我

的错误
  

BasicNetwork.performRequest:意外的响应代码400。

2 个答案:

答案 0 :(得分:0)

检查此代码作为参考,并根据您的代码进行操作。

t1 = PythonOperator(task_id='download_file_' + account['id'],
                 python_callable=download_files(account),
                 retries=3,
                 retry_delay=timedelta(seconds=300),
                 dag=my_dag)

答案 1 :(得分:0)

尝试使用Json请求而不是使用String请求

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.POST, url, null, new Response.Listener<JSONObject({

    @Override
    public void onResponse(JSONObject response) {
        mTxtDisplay.setText("Response: " + response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});