android - Send Post Request with params using Retrofit -
i'm trying unsuccessfully consume api on android using retrofit library while using postman can see expected results.
postman setting
the api url (base+controller)
http method set post
clicked from-data or x-www-form-urlencoded
then pass 2 params on key/value fields.
android retrofit setting
@post("/getdetailwithmonthwithcode") void getlandingpagereport(@query("code") string code, @query("monthact") string monthact, callback<landingpagereport> cb); @formurlencoded @post("/getdetailwithmonthwithcode") void getlandingpagereport(@field("code") string code, @field("monthact") string monthact, callback<landingpagereport> cb);
none of options works. getting {} result.
update
same settings using standard httpclient
(and httppost
) class works fine.
httpclient client = new defaulthttpclient(); httppost post = new httppost(url); list<namevaluepair> urlparameters = new arraylist<namevaluepair>(); urlparameters.add(new basicnamevaluepair("code", "testcode")); urlparameters.add(new basicnamevaluepair("monthact", "feb-2015")); post.setentity(new urlencodedformentity(urlparameters)); httpresponse response = client.execute(post);
why can't request , correct response in retrofit?
update 2
@post("/getdetailwithmonthwithcode") void getlandingpagereport(@query("code") string code, @query("monthact") string monthact, callback<list<landingpagereport>> cb); @formurlencoded @post("/getdetailwithmonthwithcode") void getlandingpagereport(@field("code") string code, @field("monthact") string monthact, callback<list<landingpagereport>>> cb);
after playing around think i've found source of problem. i've updated retrofit code receive list<landingpagereport>
. error occur
retrofit.retrofiterror: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_array begin_object @ line 1 column 2 path $
the reason consume 2 api's (webapi , wcf). other json
response arrays of objects. [{},{}] in call i've received
{ "getdetailwithmonthwithcoderesult": [ { "code": "test", "field1": "test", } ] }
but still can't manage parse response.
this simple solution not need use json
public interface registerapi { @formurlencoded @post("/retrofitexample/insert.php") public void insertuser( @field("name") string name, @field("username") string username, @field("password") string password, @field("email") string email, callback<response> callback); }
method send data
private void insertuser(){ //here handle http request insert user mysql db //creating restadapter restadapter adapter = new restadapter.builder() .setendpoint(root_url) //setting root url .build(); //finally building adapter //creating object our interface registerapi api = adapter.create(registerapi.class); //defining method insertuser of our interface api.insertuser( //passing values getting edittexts edittextname.gettext().tostring(), edittextusername.gettext().tostring(), edittextpassword.gettext().tostring(), edittextemail.gettext().tostring(), //creating anonymous callback new callback<response>() { @override public void success(response result, response response) { //on success read server's output using bufferedreader //creating bufferedreader object bufferedreader reader = null; //an string store output server string output = ""; try { //initializing buffered reader reader = new bufferedreader(new inputstreamreader(result.getbody().in())); //reading output in string output = reader.readline(); } catch (ioexception e) { e.printstacktrace(); } //displaying output toast toast.maketext(mainactivity.this, output, toast.length_long).show(); } @override public void failure(retrofiterror error) { //if error occured displaying error toast toast.maketext(mainactivity.this, error.tostring(),toast.length_long).show(); } } ); }
now can post request using php aur other server side scripting.
source android retrofit tutorial
Comments
Post a Comment