0% found this document useful (0 votes)
12 views2 pages

Main

The document contains code for an Android application that takes user input for name and age and submits it to a database using Volley. It defines layout elements, handles button clicks to get input and call a method to make a POST request submitting the data.

Uploaded by

Theo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Main

The document contains code for an Android application that takes user input for name and age and submits it to a database using Volley. It defines layout elements, handles button clicks to get input and call a method to make a POST request submitting the data.

Uploaded by

Theo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.example.

test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.TextViewCompat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

EditText editName, editAge;


Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editName = findViewById(R.id.editName);
editAge = findViewById(R.id.editAge);
submitButton = findViewById(R.id.submitButton);

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
String age = editAge.getText().toString();

submitUserToDatabase (name, age);


}
});

private void submitUserToDatabase(String name, String age) {


String url= "http://banco.co.za/TheBank/submitUser.php";

RequestQueue requestQueue = Volley.newRequestQueue(this);


StringRequest stringrequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response", response);
Toast.makeText(MainActivity.this, ""+response,
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", ""+error);
Toast.makeText(MainActivity.this, ""+error,
Toast.LENGTH_SHORT).show();

}
}){
protected HashMap<String,String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("name",name);
map.put("age",age);

return map;
}
};
requestQueue.add(stringrequest);
}
}

You might also like