json - post data to server on android -


i'm trying post data server using cakephp api. using postman api works fine on android got problem. i'm using asynctask think may have problem json post. got error message telling me that

 org.json.jsonexception: value <br of type java.lang.string cannot converted jsonobject 

this code ,could please tell me what's wrong ?

public class asyctaskmanager {      public static class addapplication extends asynctask<string, void, string> {         string result ;          @override         protected string doinbackground(string... params) {              string addflightplanurl = "http://xxx.xxx.xxx.xxx:8765/car/addapplication";             string token = "eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyjzdwiiojisimv4cci6mtuwmtczotkynh0.5dwpuoscldi3ivkuj6afpmst6d5mwuoacozq0egblay";              string postdata ;             url object = null;             try {                 object = new url(addflightplanurl);                 httpurlconnection con = (httpurlconnection) object.openconnection();                 con.setdooutput(true);                 con.setdoinput(true);                 con.setrequestproperty("authorization", token);                 con.setrequestproperty("accept", "application/json");                 con.setrequestproperty("content-type", "application/json");                  con.setrequestmethod("post");                 jsonobject cred = new jsonobject();                  cred.accumulate("code", "2");                 cred.accumulate("name", "2");                   outputstreamwriter wr = new outputstreamwriter(con.getoutputstream());                 wr.write(cred.tostring());                 wr.flush();                 wr.close();                 stringbuilder sb = new stringbuilder();                 int httpresult = con.getresponsecode();                 if (httpresult == httpurlconnection.http_ok) {                      bufferedreader br = new bufferedreader(                             new inputstreamreader(con.getinputstream(), "utf-8"));                     string line = null;                     while ((line = br.readline()) != null) {                         sb.append(line + "\n");                     }                     br.close();                     jsonobject jobj = new jsonobject(sb.tostring());                      result = jobj.tostring();                      if (result != null) {                         return result;                     }                 } else {                     system.out.println(con.getresponsemessage());                 }             } catch (malformedurlexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } catch (jsonexception e) {                 e.printstacktrace();             }             return null;         }          @override         protected void onpostexecute(string result) {             log.e("result","null "+result);         }     } } 

update

after debugging appears may have problems cakephp . got error message:

deprecated: automatically populating $http_raw_post_data deprecated , removed in future version. avoid warning set 'always_populate_raw_post_data' '-1' in php.ini , use php://input stream instead. in unknownon line 0 warning: cannot modify header information - headers sent in unknown on line 0 warning (512): unable emit headers. headers sent in file= line=0 [core/src/http/responseemitter.php, line 48] 

but don't know why works postman , it's not working android !

based on <br message of exception think server expecting header or similar postman sending , don't, server answering error page (that's why html
tag. can add facebook's stetho tool compare raw request you're sending against postman's one.

but if solve that, recommend stop using both asynctask , httpurlconnection. there're lot of tools can make life easier. think best choice if retrofit.

hope helps, regards!

edit:

  • if it's not used correctly, asynctask can give issues when rotating instance, can create memory leaks. can find lot of articles talking dos , donts of asynctask.
  • it's fine if want use httpurlconnection , parse response manually, there's no problem that, have specific stuff while there lot of people prefer using abstract libraries retrofit under hood same thing better you, , let take care of important you: business logic. it's recommendation.

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -