public class MainActivity extends AppCompatActivity {
EditText firstname, lastname, age;
Button insert, show;
TextView result;
RequestQueue requestQueue;
String insertUrl = "http://192.168.56.1:8080/tutorial/insertStudent.php";
String showUrl = "http://192.168.56.1:8080/tutorial/showStudents.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText)findViewById(R.id.firstname);
lastname = (EditText)findViewById(R.id.lastname);
lastname = (EditText)findViewById(R.id.age);
insert = (Button)findViewById(R.id.insertstudent);
show = (Button)findViewById(R.id.showstudent);
result = (TextView)findViewById(R.id.textView2);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<jsonobject>() {
@Override
public void onResponse(JSONObject response) {
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) {
Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
int socketTimeout = 30000; // 30 seconds. You can change it
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT) ;
jsonObjectRequest.setRetryPolicy(policy);
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) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_SHORT).show();
}
}) {
@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;
}
};
What I have tried:
i tried to insert the data into the database , but it shows null reference error,
can some one help me out of this,
thanks in advance