php - Android Volley - BasicNetwork.performRequest: Unexpected response code 400 -
problem: when click register button, show blank toast , didnt insert data on online database webserver.
old code on onerrorresponse:
public void onerrorresponse(volleyerror error) { progressdialog.hide(); toast.maketext(getapplicationcontext(),error.getmessage(), toast.length_long).show(); }
searched solution:
android volley - basicnetwork.performrequest: unexpected response code 400
new problem: stuck in "registering user ..." when add data , clicked register button.
logcat errors:
e/volley: [166] basicnetwork.performrequest: unexpected response code 400 *htt://hosturl.com* registeruser.php
and
e/surface: getslotfrombufferlocked: unknown buffer: 0xa2565120
source code:
package com.chishingwan.loginregister; import android.app.progressdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.android.volley.authfailureerror; import com.android.volley.networkresponse; import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.servererror; import com.android.volley.volleyerror; import com.android.volley.toolbox.httpheaderparser; import com.android.volley.toolbox.stringrequest; import com.android.volley.toolbox.volley; import org.json.jsonexception; import org.json.jsonobject; import java.io.unsupportedencodingexception; import java.util.hashmap; import java.util.map; public class registeractivity extends appcompatactivity implements view.onclicklistener { private edittext etfirstname, etlastname, etaddress, etcontactnumber, etemail, etpassword; private button bregister; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); etfirstname = (edittext) findviewbyid(r.id.etfirstname); etlastname = (edittext) findviewbyid(r.id.etlastname); etaddress = (edittext) findviewbyid(r.id.etaddress); etcontactnumber = (edittext) findviewbyid(r.id.etcontactnumber); etemail = (edittext) findviewbyid(r.id.etemail); etpassword = (edittext) findviewbyid(r.id.etpassword); bregister = (button) findviewbyid(r.id.bregister); progressdialog = new progressdialog(this); bregister.setonclicklistener(this); } private void registeruser(){ final string firstname = etfirstname.gettext().tostring().trim(); final string lastname = etlastname.gettext().tostring().trim(); final string contactnumber = etcontactnumber.gettext().tostring().trim(); final string address = etaddress.gettext().tostring().trim(); final string email = etemail.gettext().tostring().trim(); final string password = etpassword.gettext().tostring().trim(); progressdialog.setmessage("registering user ... "); progressdialog.show(); stringrequest stringrequest = new stringrequest(request.method.post, constants.url_register, new response.listener<string>() { @override public void onresponse(string response) { progressdialog.dismiss(); try{ jsonobject jsonobject = new jsonobject(response); toast.maketext(getapplicationcontext(), jsonobject.getstring("message"), toast.length_long).show(); }catch(jsonexception e){ e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { networkresponse response = error.networkresponse; if (error instanceof servererror && response != null) { try { string res = new string(response.data, httpheaderparser.parsecharset(response.headers, "utf-8")); // can use deserializer make sense of data jsonobject obj = new jsonobject(res); } catch (unsupportedencodingexception e1) { // couldn't decode data string e1.printstacktrace(); } catch (jsonexception e2) { // returned data not jsonobject? e2.printstacktrace(); } } } }){ @override protected map<string, string> getpostparams() throws authfailureerror { map<string, string> params = new hashmap<>(); params.put("firstname", firstname); params.put("lastname", lastname); params.put("address", address); params.put("contactnumber", contactnumber); params.put("email", email); params.put("password", password); return params; } }; requestqueue requestqueue = volley.newrequestqueue(this); requestqueue.add(stringrequest); } @override public void onclick(view view) { if(view==bregister){ registeruser(); } } }
you forgot add header stringrequest
:
@override public map<string, string> getheaders() throws authfailureerror { map<string, string> header = new linkedhashmap<string, string>(); header.put("content-type", "application/x-www-form-urlencoded"); return super.getheaders(); }
Comments
Post a Comment