は([ボレーを使用してJSONデータをPOSTリクエストを送信]の
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("email", "[email protected]");
jsonObject.put("password", "abcd123");
jsonObject.put("device", "jdghfdhgdhi");
jsonObject.put("latitude", 1.2456);
jsonObject.put("longitude", 1.3466);
} catch (JSONException e) {
e.printStackTrace();
}
final JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// here parse your response json data
try {
String status = response.getString("XSTS");
} catch (JSONException e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button.
// For AuthFailure, you can re login with user credentials.
// In this case you can check how client is forming the api and debug accordingly.
// For ServerError 5xx, you can do retry or handle accordingly.
if (error instanceof NetworkError) {
Toast.makeText(LoginScreenActivity.this, "No internet connection", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(LoginScreenActivity.this, "Our server is busy please try again later", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(LoginScreenActivity.this, "AuthFailure Error please try again later", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(LoginScreenActivity.this, "Parse Error please try again later", Toast.LENGTH_SHORT).show();
} else if (error instanceof NoConnectionError) {
Toast.makeText(LoginScreenActivity.this, "No connection", Toast.LENGTH_SHORT).show();
} else if (error instanceof TimeoutError) {
Toast.makeText(LoginScreenActivity.this, "Server time out please try again later", Toast.LENGTH_SHORT).show();
}
error.printStackTrace();
progressDialog.dismiss();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
Volley.newRequestQueue(LoginScreenActivity.this).add(postRequest);
可能な重複し、このコードを試してみてくださいhttp://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) – Sanoop
http://stackoverflow.com/ques 23220695/send-post-request with json-data-using-volleyその回答を見てください –
http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-ボレー/ボレーについて –