TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mohsin Arif
311
6k
137.7k
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.
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();
//tv_volley_result.setText(s);
}
},
new
Response.ErrorListener() {
@Override
public
void
onErrorResponse(VolleyError volleyError) {
Toast.makeText(getContext(),
"request was aborted"
+ volleyError, Toast.LENGTH_LONG).show();
//tv_volley_result.setText("request was aborted" + volleyError);
}
}) {
@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;
}
};
// 3 take post Request added to queue
mQueue.add(stringRequest);
}
}
Reply
Answers (
1
)
Save to existing database
How to enable AndroidX dependencies