java - Android Posting JSON to PHP -


i trying send json post array php server. array contains separate objects/tickets php decode. android loads data json object local database fine. can log data it's pulling , see before it's sent php server. on server side data doesn't make it. it's sending empty array.

my code:

public void sendqueuedtickets() {     // check queue tickets.     if (dbc.checkquetickets() != null) {         string[] userinfo;         cursor cursor = dbc.checkquetickets();         int columns = cursor.getcolumncount();         int rows = cursor.getcount();         final arraylist<integer> queueids = new arraylist<>();         jsonarray arr = new jsonarray();         cursor.movetofirst();         // user info.         userinfo = dbc.checkcredentials();         // add user info , request type params server knows we're doing.         jsonobject jobj = new jsonobject();         try {             jobj.put("username", userinfo[0]);             jobj.put("serveruserid", userinfo[2]);             jobj.put("request", "submitticket");         } catch (jsonexception e) { e.printstacktrace(); }         // add sql tickets params sent.         (int r = 0; r < rows; r++) {             (int c = 0; c < columns; c++) {                 try {                     jobj.put(cursor.getcolumnname(c), cursor.getstring(c));                 } catch (jsonexception e) { e.printstacktrace(); }             }             arr.put(jobj);             queueids.add(cursor.getint(0)); // id column value.             cursor.movetonext();         }         // send ticket(s) remote server.         // set timeout parameters.         httpparams httpparameters = new basichttpparams();         httpconnectionparams.setconnectiontimeout(httpparameters, 5000);         httpconnectionparams.setsotimeout(httpparameters, 5000);         // continue http request.         httpclient client = new defaulthttpclient(httpparameters);         httpcontext httpcontext = new basichttpcontext();         httppost httppost = new httppost("http://" + userinfo[3] + "." + dbc.remote_server_url);         try {             stringentity se = new stringentity(arr.tostring());             httppost.setentity(se);             httppost.setheader("accept", "application/json");             httppost.setheader("content-type", "application/json");              httpresponse response = client.execute(httppost, httpcontext);             string resfromserver = org.apache.http.util.entityutils.tostring(response.getentity());             system.out.println(resfromserver);             // delete queued ticket(s) if sent transaction successful.             if (resfromserver.contains("successfulsubmit")) {                 (int r = 0; r < rows; r++) {                     dbc.deletequeticket(queueids.get(r));                 }             }         } catch (exception e) { e.printstacktrace(); }     } } 

this code use in php server side json data android:

$json = file_get_contents('php://input'); $obj = json_decode($json,true);//true means convert 2d array 

and code use sending android:

private void sendhttp(jsonarray json) throws jsonexception, ioexception  {         httpparams httpparams = new basichttpparams();      httpconnectionparams.setconnectiontimeout(httpparams, 30000);     httpconnectionparams.setsotimeout(httpparams, 10000);       httpclient client = new defaulthttpclient(httpparams);      string url = "your url write here";     log.i("","http post on url");     httppost request = new httppost(url);//send httppost string url json format        request.setentity(new bytearrayentity(json.tostring().getbytes(             "utf8")));      request.setheader("json", json.tostring());     request.setheader("accept", "application/json");      request.setheader("content-type", "application/json");     log.i("", "excuting request");     httpresponse response =client.execute(request);//getting response      //getting response     httpentity entity = response.getentity();         inputstream = null;         stringbuilder sb=null;         is=entity.getcontent();         bufferedreader reader = new bufferedreader(new       inputstreamreader(is,"iso-8859-1"),10);         sb = new stringbuilder();         sb.append(reader.readline() + "\n");         string line="0";          while ((line = reader.readline()) != null) {             sb.append(line + "\n");         }          is.close();           string result=sb.tostring();         jsonarray res=new jsonarray(result);//the response json array   } 

the code works 100%.


Comments