java - Getting responsecode of 400 from POST in Android Studio -
i've been following wintech's youtube tutorials this. set , added few post methods. methods work fine, when try posting bits, 400 response code. know means improper/malformed syntax, cannot find is. it's been few hours of trying , searching. , yes, aware can in 1 class , method, did see bit causing problem. it's of them. can tell me bit might wrong?
mainactivity.java:
import android.app.activity; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.content.intent; import android.net.uri; import android.widget.adapterview; import android.widget.checkbox; import android.widget.datepicker; import android.widget.edittext; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.lang.reflect.array; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.hashmap; import android.os.asynctask; import android.widget.tablelayout; import android.widget.textview; import android.widget.timepicker; import android.widget.toast; import org.json.jsonarray; import org.json.jsonobject; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string choice = getintent().getstringextra("connumchoice"); textview controlnumber = (textview) findviewbyid(r.id.apptnumview); controlnumber.settext(choice); } public void openwebpage(view view){ uri weblink = uri.parse("http://www.mysteelsoftware.com"); intent weblinkintent = new intent(intent.action_view, weblink); startactivity(weblinkintent); } public void onrefresh(view view){ intent backtoappt = new intent(mainactivity.this, appointments.class); startactivity(backtoappt); } public void onrecinv(view view){ } public void onsave(view view){ checkbox truckarr = (checkbox) findviewbyid(r.id.truckarrivedbox); boolean truckhasarrived; if (truckarr.ischecked()){ truckhasarrived = true; }else{ truckhasarrived = false; } new saveconnum().execute("http://192.168.xxx.xxx/localapptest/service1.svc/setcontrolnumber"); new addarrivaldate().execute("http://192.168.xxx.xxx/localapptest/service1.svc/addarrivaldate"); new addarrivaltime().execute("http://192.168.xxx.xxx/localapptest/service1.svc/addar rivaltime"); } public void oncancel(view view){ edittext poinput = (edittext) findviewbyid(r.id.poninput); edittext relnuminput = (edittext) findviewbyid(r.id.relnuminput); edittext railcarinput = (edittext) findviewbyid(r.id.railcarinput); checkbox truckarr = (checkbox) findviewbyid(r.id.truckarrivedbox); edittext billlad = (edittext) findviewbyid(r.id.billnuminput); poinput.settext(""); relnuminput.settext(""); railcarinput.settext(""); truckarr.setchecked(false); billlad.settext(""); } class saveconnum extends asynctask<string, void, string> { stringbuilder sb = new stringbuilder(); string status = null; activity context; textview controlnumview = (textview) findviewbyid(r.id.apptnumview); edittext poinput = (edittext) findviewbyid(r.id.poninput); edittext relnuminput = (edittext) findviewbyid(r.id.relnuminput); edittext railcarinput = (edittext) findviewbyid(r.id.railcarinput); edittext billlad = (edittext) findviewbyid(r.id.billnuminput); datepicker datepick = (datepicker) findviewbyid(r.id.datepicker); timepicker timepick = (timepicker) findviewbyid(r.id.arrivaltimeinput); string connum = controlnumview.gettext().tostring(); string ponum = poinput.gettext().tostring(); string relnum = relnuminput.gettext().tostring(); string railcarnum = railcarinput.gettext().tostring(); string billnum = billlad.gettext().tostring(); int year = datepick.getyear(); int month = datepick.getmonth(); int day = datepick.getdayofmonth(); string date = "" + year + "" + month + "" + day + ""; integer dateint = integer.valueof(date); int hour = timepick.getcurrenthour(); int minute = timepick.getcurrentminute(); string time = "" + hour + "" + minute + ""; protected void onpreexecute(){ } public string doinbackground(string... connurl){ httpurlconnection conn = null; bufferedreader reader; try{ final url url = new url(connurl[0]); conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setdoinput(true); conn.setchunkedstreamingmode(0); conn.addrequestproperty("content-type", "application/json; charset=utf-8"); conn.setrequestmethod("post"); jsonobject jsonobject = new jsonobject(); jsonobject.put("connumber", connum); outputstream out = new bufferedoutputstream(conn.getoutputstream()); out.write(jsonobject.tostring().getbytes()); out.flush(); out.close(); int result = conn.getresponsecode(); if(result == 200){ inputstream in = new bufferedinputstream(conn.getinputstream()); reader = new bufferedreader(new inputstreamreader(in)); string line = null; while ((line = reader.readline()) != null){ status = line; } } }catch(exception ex){ } return status; } public void onpostexecute(string result){ super.onpostexecute(result); if(result != null){ toast.maketext(mainactivity.this, "saved successfully. connum", toast.length_short).show(); }else{ toast.maketext(mainactivity.this, "save failed. connum", toast.length_short).show(); } } } class addarrivaldate extends asynctask<string, void, string> { stringbuilder sb = new stringbuilder(); string status = null; activity context; textview controlnumview = (textview) findviewbyid(r.id.apptnumview); edittext poinput = (edittext) findviewbyid(r.id.poninput); edittext relnuminput = (edittext) findviewbyid(r.id.relnuminput); edittext railcarinput = (edittext) findviewbyid(r.id.railcarinput); edittext billlad = (edittext) findviewbyid(r.id.billnuminput); datepicker datepick = (datepicker) findviewbyid(r.id.datepicker); timepicker timepick = (timepicker) findviewbyid(r.id.arrivaltimeinput); string connum = controlnumview.gettext().tostring(); string ponum = poinput.gettext().tostring(); string relnum = relnuminput.gettext().tostring(); string railcarnum = railcarinput.gettext().tostring(); string billnum = billlad.gettext().tostring(); int year = datepick.getyear(); int month = datepick.getmonth(); int day = datepick.getdayofmonth(); string date = "" + year + "" + month + "" + day + ""; integer dateint = integer.valueof(date); int hour = timepick.getcurrenthour(); int minute = timepick.getcurrentminute(); string time = "" + hour + "" + minute + ""; protected void onpreexecute(){ } public string doinbackground(string... connurl){ httpurlconnection conn = null; bufferedreader reader; try{ final url url = new url(connurl[0]); conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setdoinput(true); conn.setchunkedstreamingmode(0); conn.addrequestproperty("content-type", "application/json; charset=utf-8"); conn.setrequestmethod("post"); jsonobject jsonobject = new jsonobject(); jsonobject.put("arrivaldate", dateint); outputstream out = new bufferedoutputstream(conn.getoutputstream()); out.write(jsonobject.tostring().getbytes()); out.flush(); out.close(); int result = conn.getresponsecode(); if(result == 200){ inputstream in = new bufferedinputstream(conn.getinputstream()); reader = new bufferedreader(new inputstreamreader(in)); string line = null; while ((line = reader.readline()) != null){ status = line; } } }catch(exception ex){ } return status; } public void onpostexecute(string result){ super.onpostexecute(result); if(result != null){ toast.maketext(mainactivity.this, "saved successfully. date", toast.length_short).show(); }else{ toast.maketext(mainactivity.this, "save failed. date", toast.length_short).show(); } } } class addarrivaltime extends asynctask<string, void, string> { stringbuilder sb = new stringbuilder(); string status = null; activity context; textview controlnumview = (textview) findviewbyid(r.id.apptnumview); edittext poinput = (edittext) findviewbyid(r.id.poninput); edittext relnuminput = (edittext) findviewbyid(r.id.relnuminput); edittext railcarinput = (edittext) findviewbyid(r.id.railcarinput); edittext billlad = (edittext) findviewbyid(r.id.billnuminput); datepicker datepick = (datepicker) findviewbyid(r.id.datepicker); timepicker timepick = (timepicker) findviewbyid(r.id.arrivaltimeinput); string connum = controlnumview.gettext().tostring(); string ponum = poinput.gettext().tostring(); string relnum = relnuminput.gettext().tostring(); string railcarnum = railcarinput.gettext().tostring(); string billnum = billlad.gettext().tostring(); int year = datepick.getyear(); int month = datepick.getmonth(); int day = datepick.getdayofmonth(); string date = "" + year + "" + month + "" + day + ""; integer dateint = integer.valueof(date); int hour = timepick.getcurrenthour(); int minute = timepick.getcurrentminute(); string time = "" + hour + "" + minute + ""; protected void onpreexecute(){ } public string doinbackground(string... connurl){ httpurlconnection conn = null; bufferedreader reader; try{ final url url = new url(connurl[0]); conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setdoinput(true); conn.setchunkedstreamingmode(0); conn.addrequestproperty("content-type", "application/json; charset=utf-8"); conn.setrequestmethod("post"); jsonobject jsonobject = new jsonobject(); jsonobject.put("arrivaltime", time); outputstream out = new bufferedoutputstream(conn.getoutputstream()); out.write(jsonobject.tostring().getbytes()); out.flush(); out.close(); int result = conn.getresponsecode(); if(result == 200){ inputstream in = new bufferedinputstream(conn.getinputstream()); reader = new bufferedreader(new inputstreamreader(in)); string line = null; while ((line = reader.readline()) != null){ status = line; } } }catch(exception ex){ } return status; } public void onpostexecute(string result){ super.onpostexecute(result); if(result != null){ toast.maketext(mainactivity.this, "saved successfully. time", toast.length_short).show(); }else{ toast.maketext(mainactivity.this, "save failed. time", toast.length_short).show(); } } } }
wcfservice
iservice1.cs
using system.collections.generic; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; namespace wcfservice2 { // note: can use "rename" command on "refactor" menu change interface name "iservice1" in both code , config file together. [servicecontract] public interface iservice1 { //get method// [operationcontract] [webget(uritemplate = "getconnum")] list<controlnumber> getconnum(); //get method// [operationcontract] [webget(uritemplate = "getcredate")] list<createddate> getcredate(); //post method// [operationcontract] [webinvoke(method = "post", uritemplate = "addarrivaldate", bodystyle = webmessagebodystyle.wrappedrequest, requestformat = webmessageformat.json, responseformat = webmessageformat.json)] int addarrivaldate(string arrivaldate); //post method// [operationcontract] [webinvoke(method = "post", uritemplate = "addarrivaltime", bodystyle = webmessagebodystyle.wrappedrequest, requestformat = webmessageformat.json, responseformat = webmessageformat.json)] int addarrivaltime(string arrivaltime); //post method// [operationcontract] [webinvoke(method = "post", uritemplate = "setcontrolnumber", bodystyle = webmessagebodystyle.wrappedrequest, requestformat = webmessageformat.json, responseformat = webmessageformat.json)] int setcontrolnumber(int connumber); } [datacontract] public class controlnumber { string connum; [datamember] public string controlnumber { { return connum; } set { connum = value; } } } [datacontract] public class createddate { string credate; [datamember] public string createddate { { return credate; } set { credate = value; } } } }
service1.svc.cs
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; using system.text; using system.data; using system.data.sqlclient; namespace wcfservice2 { // note: can use "rename" command on "refactor" menu change class name "service1" in code, svc , config file together. // note: in order launch wcf test client testing service, please select service1.svc or service1.svc.cs @ solution explorer , start debugging. public class service1 : iservice1 { //connection string// private string constr = "data source=xxx\\xxx;initial catalog=xxxxx;user id=xxxxx;password=xxxxx"; public string addconnum(int connum) { throw new notimplementedexception(); } public list<controlnumber> getconnum() { list<controlnumber> connumlist = new list<controlnumber>(); sqlconnection connection = new sqlconnection(this.constr); connection.open(); sqlcommand cmd = new sqlcommand("select top 20 controlnumber apptin createdby = 'kiosk' order controlnumber desc", connection); cmd.commandtype = commandtype.text; sqldatareader sdr = cmd.executereader(); while (sdr.read()) { controlnumber connum = new controlnumber(); connum.controlnumber = sdr["controlnumber"].tostring(); connumlist.add(connum); } return connumlist.tolist(); } public list<createddate> getcredate() { list<createddate> credatelist = new list<createddate>(); sqlconnection connection = new sqlconnection(this.constr); connection.open(); sqlcommand cmd = new sqlcommand("select top 20 dbo.cladate(createddate) createddate apptin createdby = 'kiosk' order controlnumber desc", connection); cmd.commandtype = commandtype.text; sqldatareader sdr = cmd.executereader(); while (sdr.read()) { createddate credate = new createddate(); credate.createddate = sdr["createddate"].tostring(); credatelist.add(credate); } return credatelist.tolist(); } public int addarrivaldate(string arrivaldate) { int status = 0; sqlconnection connection = new sqlconnection(this.constr); sqlcommand cmd = new sqlcommand(); try { if (connection.state == connectionstate.closed) { connection.open(); } cmd = new sqlcommand("insert apptin (arrivaldate, arrivaltime) values (@arrivaldate, @arrivaltime)", connection); cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@arrivaldate", arrivaldate); cmd.executereader(); status = 1; } catch(exception ex) { throw ex; } { connection.close(); cmd.dispose(); } return status; } public int addarrivaltime(string arrivaltime) { int status = 0; sqlconnection connection = new sqlconnection(this.constr); sqlcommand cmd = new sqlcommand(); try { if (connection.state == connectionstate.closed) { connection.open(); } cmd = new sqlcommand("insert apptin (arrivaltime) values (@arrivaltime)", connection); cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@arrivaltime", arrivaltime); cmd.executereader(); status = 1; } catch (exception ex) { throw ex; } { connection.close(); cmd.dispose(); } return status; } public int setconnum(int connumber) { int status = 0; sqlconnection connection = new sqlconnection(this.constr); sqlcommand cmd = new sqlcommand(); try { if (connection.state == connectionstate.closed) { connection.open(); } cmd = new sqlcommand("insert apptin (controlnumber) values (@connum)", connection); cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@connum", connumber); cmd.executereader(); status = 1; } catch (exception ex) { throw ex; } { connection.close(); cmd.dispose(); } return status; } public int setcontrolnumber(int connumber) { throw new notimplementedexception(); } } }
when try , view in=browser let me know if haven't provided sufficient information.. or if it's obvious.
i knew stupid. needed update sql command in web service, not insert.
Comments
Post a Comment