Mohsin Arif

Mohsin Arif

  • 309
  • 6k
  • 138.5k

Onclick Post data in Database?

Mar 22 2020 2:29 PM
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.
  1. public class addEmailFragment extends Fragment {  
  2.     private AddEmailViewModel mViewModel;  
  3.     TextView txtName;  
  4.     TextView txtEmail;  
  5.     CheckBox chEverDay;  
  6.     CheckBox chMonthly;  
  7.     public RequestQueue mQueue;  
  8.     public View onCreateView(@NonNull LayoutInflater inflater,  
  9.                              ViewGroup container, Bundle savedInstanceState) {  
  10.         mViewModel = ViewModelProviders.of(this).get(AddEmailViewModel.class);  
  11.         View root = inflater.inflate(R.layout.add_email_fragment, container, false);  
  12.         mQueue = Volley.newRequestQueue(root.getContext());  
  13.         Button  buttonEmail=root.findViewById(R.id.btnSaveEmail);  
  14.         txtName   =root.findViewById(R.id.txtName);  
  15.         txtEmail  =root.findViewById(R.id.txtEmail);  
  16.         chEverDay =root.findViewById(R.id.ch_EveryDayReport);  
  17.         chMonthly =root.findViewById(R.id.ch_MonthlyReport);  
  18.         buttonEmail.setOnClickListener(new View.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 String Name=txtName.getText().toString();  
  22.                 String Email=txtEmail.getText().toString();  
  23.                 PostData(Name,Email);  
  24.             }  
  25.         });  
  26.         return root;  
  27.   
  28.     }  
  29.   
  30.     public void PostData(String EName,String EEmail) {  
  31.         String url = "http://marjanjewellers.co/bilal/api_insert_emails.php";  
  32.         StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {  
  33.             @Override  
  34.             public void onResponse(String s) {  
  35.                 Toast.makeText(getContext(), s, Toast.LENGTH_LONG).show();  
  36.                 //tv_volley_result.setText(s);  
  37.             }  
  38.         }, new Response.ErrorListener() {  
  39.             @Override  
  40.             public void onErrorResponse(VolleyError volleyError) {  
  41.                 Toast.makeText(getContext(), "request was aborted" + volleyError, Toast.LENGTH_LONG).show();  
  42.                 //tv_volley_result.setText("request was aborted" + volleyError);  
  43.             }  
  44.         }) {  
  45.             @Override  
  46.             protected Map<String, String> getParams() throws AuthFailureError {  
  47.   
  48.                 Map<String, String> map = new HashMap<String, String>();  
  49.                 map.put("name","mohs");  
  50.                 map.put("email","[email protected]");  
  51.                 map.put("everyday_report","1");  
  52.                 map.put("monthly_report","1");  
  53.   
  54.                 return map;  
  55.             }  
  56.         };  
  57.         // 3 take post Request added to queue  
  58.         mQueue.add(stringRequest);  
  59.     }  
  60.   
  61. }  
 

Answers (1)