Skip to content

Instantly share code, notes, and snippets.

@fnzainal
Created December 1, 2015 09:44
Show Gist options
  • Save fnzainal/522fe8bd2ce6bda17e3c to your computer and use it in GitHub Desktop.
Save fnzainal/522fe8bd2ce6bda17e3c to your computer and use it in GitHub Desktop.
request json data with volley
private void getDataRequest() {
String url= "";
RequestQueue queue = Volley.newRequestQueue(getActivity());
StringRequest postRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
progress.dismiss();
Log.d("response", response);
try {
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();
ResultJson dataResult = gson.fromJson(response, ResultJson.class);
modelSurvey = dataResult.getSurvey();
Adapter adapter = new Adapter(
getContext(), modelSurvey);
lvSurvey = (ListView)getActivity().findViewById(R.id.lvSurvey);
lvSurvey.setAdapter(adapter);
lvSurvey.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//task when clicked
}
});
}catch (Exception e){
Log.d("error todolist", Log.getStackTraceString(e));
Toast.makeText(getActivity(), "Error load data", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
Log.d("ERROR", "error => " + error.toString());
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
};
queue.add(postRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment