php - Retrofit 2 response body contents only "<html>" string -
i'm trying understand how use retrofit library, created android project , simple php script. index.php file location xampp's htdocs directory.
here php script, here want return string when script executed
<html> <head> <title>php test</title> </head> <body> <?php echo "success"; ?> </body> </html>
on android part want send request
@get("/") call<string> test();
here send response , necessary preparations
gson gson = new gsonbuilder() .setdateformat("yyyy-mm-dd't'hh:mm:ssz") .setlenient() .create(); retrofit retrofit = new retrofit.builder() .baseurl("http://192.168.43.169:80/") .addconverterfactory(gsonconverterfactory.create(gson)) .build(); idatasyncapi datasyncapi = retrofit.create(idatasyncapi.class); callback<string> callback = new callback<string>() { @override public void onresponse(call<string> call, response<string> response) { string responsestr = gson.fromjson(response.body().tostring(), string.class); log.d("response", responsestr); } @override public void onfailure(call<string> call, throwable t) { } }; call<string> response = datasyncapi.test(); response.enqueue(callback);
the problem response's body equals "< html >". doing wrong?
update 1
i tried using wikipedia public api instead of mine, , result same. got ""
@get("/w/api.php") call<string> test(@querymap map<string, string> argsmap); retrofit retrofit = new retrofit.builder() .baseurl("https://en.wikipedia.org/") .addconverterfactory(gsonconverterfactory.create(gson)) .build(); map<string, string> args = new hashmap<string, string>(); args.put("action", "query"); args.put("meta", "siteinfo"); args.put("siprop", "namespaces"); call<string> response = datasyncapi.test(args); response.enqueue(callback);
i figured out myself. problem used string type call class (call<string>
). instead, better use call<responsebody>
, becomes this:
service
@get("/w/api.php") call<responsebody> test(@querymap map<string, string> argsmap);
making request
call<responsebody> response = datasyncapi.test(args); response.enqueue(callback);
Comments
Post a Comment