//for get method
private JSONObject jObj;
private void volleyGetMethod() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
YOUR_URL, null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
jObj = new JSONObject(response.toString());
// rest of your code here
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// System.out.println(error.getMessage());
}
}) {
/**
* Passing some request headers
*/
@Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(activity).add(jsonObjReq);
}
// for post method
private void volleyPostMethod() {
JSONObject obj = new JSONObject();
try {
obj.put("number", "");
// add other parameter here..
} catch (Exception e) {
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
YOUR_URL, obj,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.println("res -- " + response.toString());
JSONObject jsonObject = new JSONObject(response
.toString());
// rest of your code here
} catch (Exception e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// rest of your code here
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// show Dialog for error
}
}) {
/**
* Passing some request headers
*/
@Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(activity).add(jsonObjReq);
}
private JSONObject jObj;
private void volleyGetMethod() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
YOUR_URL, null,
new Response.Listener
@Override
public void onResponse(JSONObject response) {
try {
jObj = new JSONObject(response.toString());
// rest of your code here
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// System.out.println(error.getMessage());
}
}) {
/**
* Passing some request headers
*/
@Override
public Map
HashMap
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(activity).add(jsonObjReq);
}
// for post method
private void volleyPostMethod() {
JSONObject obj = new JSONObject();
try {
obj.put("number", "");
// add other parameter here..
} catch (Exception e) {
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
YOUR_URL, obj,
new Response.Listener
@Override
public void onResponse(JSONObject response) {
try {
System.out.println("res -- " + response.toString());
JSONObject jsonObject = new JSONObject(response
.toString());
// rest of your code here
} catch (Exception e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// rest of your code here
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// show Dialog for error
}
}) {
/**
* Passing some request headers
*/
@Override
public Map
HashMap
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(activity).add(jsonObjReq);
}
No comments:
Post a Comment