hello guys
i want to post when i click on my button then save data in database through api, i m using volley but this not done, please correct me where i am wrong below is my code of whole activity.
- public class addEmailFragment extends Fragment {
- private AddEmailViewModel mViewModel;
- TextView txtName;
- TextView txtEmail;
- CheckBox chEverDay;
- CheckBox chMonthly;
- public RequestQueue mQueue;
- public View onCreateView(@NonNull LayoutInflater inflater,
- ViewGroup container, Bundle savedInstanceState) {
- mViewModel = ViewModelProviders.of(this).get(AddEmailViewModel.class);
- View root = inflater.inflate(R.layout.add_email_fragment, container, false);
- mQueue = Volley.newRequestQueue(root.getContext());
- Button buttonEmail=root.findViewById(R.id.btnSaveEmail);
- txtName =root.findViewById(R.id.txtName);
- txtEmail =root.findViewById(R.id.txtEmail);
- chEverDay =root.findViewById(R.id.ch_EveryDayReport);
- chMonthly =root.findViewById(R.id.ch_MonthlyReport);
- buttonEmail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String Name=txtName.getText().toString();
- String Email=txtEmail.getText().toString();
- PostData(Name,Email);
- }
- });
- return root;
-
- }
-
- public void PostData(String EName,String EEmail) {
- String url = "http://marjanjewellers.co/bilal/api_insert_emails.php";
- StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
- @Override
- public void onResponse(String s) {
- Toast.makeText(getContext(), s, Toast.LENGTH_LONG).show();
-
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError volleyError) {
- Toast.makeText(getContext(), "request was aborted" + volleyError, Toast.LENGTH_LONG).show();
-
- }
- }) {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
-
- Map<String, String> map = new HashMap<String, String>();
- map.put("name","mohs");
- map.put("email","[email protected]");
- map.put("everyday_report","1");
- map.put("monthly_report","1");
-
- return map;
- }
- };
-
- mQueue.add(stringRequest);
- }
-
- }