我有一个服务器端api方法,该方法接受Model类作为参数
哪一个工作正常,我已经检查过了,但是从android上我很困惑如何将Model类作为参数发送?
public class Login extends AppCompatActivity {
EditText email, password;
String Email, Pass;
Button loginbtn;
String URL = "https://192.168.0.102/api/campaign/Logins";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
handleSSLHandshake();
email = findViewById(R.id.input_email);
password = findViewById(R.id.password);
loginbtn = findViewById(R.id.login_button);
final RequestQueue queue = Volley.newRequestQueue(this);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Email = email.getText().toString();
Pass = password.getText().toString();
StringRequest postRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.toString());
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("email", Email);
params.put("password", Pass);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
queue.add(postRequest);
}
});
这是我尝试过的,我知道问题出在服务器端,任何人都可以帮助我我所认为的问题吗?
答案 0 :(得分:0)
您只需要在JSON对象中添加带有post参数作为键值对的JSONObject。这是适合您的示例:
JSONObject postparams = new JSONObject();
postparams.put("keyX", "valueX")
postparams.put("KEY", "Value")
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, postparams,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
//Success Callback
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Failure Callback
}
});