Skip to content

Instantly share code, notes, and snippets.

@serdoune
Created January 31, 2018 20:37
Show Gist options
  • Save serdoune/0eca3b9960933eeb7261962c23701507 to your computer and use it in GitHub Desktop.
Save serdoune/0eca3b9960933eeb7261962c23701507 to your computer and use it in GitHub Desktop.
final EditText firstname, lastname, age;
Button insert, show;
final RequestQueue requestQueue;
final String insertUrl = "http://192.168.1.65/tutorial/insertStudent.php";
final String showUrl = "http://192.168.1.65/tutorial/showStudents.php";
final TextView result;
firstname = (EditText) findViewById(R.id.editText);
lastname = (EditText) findViewById(R.id.editText2);
age = (EditText) findViewById(R.id.editText3);
insert = (Button) findViewById(R.id.insert);
show = (Button) findViewById(R.id.showstudents);
result = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("ww");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
String lastname = student.getString("lastname");
String age = student.getString("age");
result.append(firstname + " " + lastname + " " + age + " \n");
}
result.append("===\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("firstname", firstname.getText().toString());
parameters.put("lastname", lastname.getText().toString());
parameters.put("age", age.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment