I want to make a JsonObjectRequest using POST method with volley library so that i can save my data on the server.
For the same, i've an API of jsonObject as follows:
{"settings":{"notification":30,"time":4}}
The java code for the same is as follows ;
public void sendrequest(String urlset)
{
RequestQueue requestQueue1=Volley.newRequestQueue(this);
JSONObject json=new JSONObject();
JsonObjectRequest jsonObjectRequest1=new JsonObjectRequest(Request.Method.POST, urlset, json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//code for getting some response from the method
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//code in order to check the error
}
});
}
How can this be done so that I am able to post my data on the server using the above code (for reference).
Thanks in advance.
After a few attempts i tried doing this:
public void sendrequest(String urlset)
{
RequestQueue requestQueue1=Volley.newRequestQueue(this);
String a,b;
SharedPreferences sharedPreferences1=getSharedPreferences("MyPreferences",MODE_PRIVATE);
a=sharedPreferences1.getString("list_preference_1","");
b=sharedPreferences1.getString("list_preference_2","");
//a=sharedPreferences1.getInt("list_preference_1", 1);
//b=sharedPreferences1.getInt("list_preference_2", 1);
JSONObject json=new JSONObject();
try {
//JSONObject ob=json.getJSONObject("settings");
json.put("notification",Integer.parseInt(a));
json.put("time",Integer.parseInt(b));
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(this, "Error 1", Toast.LENGTH_SHORT).show();
}
JSONObject jsonfinal=new JSONObject();
try {
jsonfinal.put("settings",json);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(this, "Error 2", Toast.LENGTH_SHORT).show();
}
JsonObjectRequest jsonObjectRequest1=new JsonObjectRequest(Request.Method.POST, urlset, jsonfinal, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(SettingsActivity.this, "Changes made to server", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SettingsActivity.this, "Check internet connectivity", Toast.LENGTH_SHORT).show();
}
});
requestQueue1.add(jsonObjectRequest1);
}
But now my application is now crashing. Can someone help me out with this?