我想知道如何在凌空环境中张贴请求对象
class Request {
int restId;
List<Item> items;
}
class Item{
int itemId;
int count;
}
答案 0 :(得分:0)
OnCreate():
RequestQueue requestQueue = Volley.newRequestQueue(this);
示例发布方法:
StringRequest postRequest = new StringRequest(Request.Method.POST, url1,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
try
{
System.out.println("response: " + response);
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
System.out.println("success: " + success);
// success-e gore usere info gosterilir
if (success.equals("true"))
{
Toast.makeText(SignUpActivity.this, R.string.register_success, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUpActivity.this, SignInActivity.class);
startActivity(intent);
}
else
{
String errorMessage = jsonObject.getString("message");
Toast.makeText(SignUpActivity.this, R.string.register_failed, Toast.LENGTH_SHORT).show();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(SignUpActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
)
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("name", nameText);
params.put("login", userNameText);
params.put("email", emailText);
params.put("password", passwordText);
if (!dateText.equals(""))
params.put("birthday", dateText);
return params;
}
};
requestQueue.add(postRequest);