problem POSTing android JSONObject to PHP -


i having trouble getting android post of simple jsonobject show in $_post data on server. server php 5.3.4 , android side sdk 8 emulator. can post simple namevaluepair normal , shows when switch jsonobject + stringentity see below $_post array shows { }. go ahead , run code below against test php page. has var_dump of $_post , $_server searching 1 of expected keys ('email'). see have tried numerous 'contenttype's see if problem. i've used wireshark verify tcp conversation looks between client , server. post data in there isn't showing in server's vars. stuck... can offer.

import java.io.inputstream;  import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.httpconnectionparams; import org.json.jsonobject;  import android.util.log;  public class testpost {     protected static void sendjson (final string email, final string pwd) {         httpclient client = new defaulthttpclient();         httpconnectionparams.setconnectiontimeout(client.getparams(), 10000); //timeout limit         httpresponse response;         string url = "http://web-billings.com/testpost.php";         try{             httppost post = new httppost(url);              // namevaluepair working fine...             //list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);               //namevaluepairs.add(new basicnamevaluepair("email", email));               //namevaluepairs.add(new basicnamevaluepair("password", pwd));               //post.setentity(new urlencodedformentity(namevaluepairs));             //log.i("main", "p2db - string entity 'se' = "+namevaluepairs.tostring());              jsonobject jobject = new jsonobject();             jobject.put("email", email);             jobject.put("password", pwd);             stringentity se = new stringentity(jobject.tostring());             //se.setcontenttype("charset=utf-8");             se.setcontenttype("application/json;charset=utf-8");             //se.setcontenttype("application/json");             //se.setcontenttype("application/x-www-form-urlencoded");              post.setentity(se);             log.i("main", "testpost - string entity 'se' = "+getinvoices.convertstreamtostring(se.getcontent()));              response = client.execute(post);                /*checking response */             if(response!=null){                 inputstream in = response.getentity().getcontent(); //get data in entity                 string message = getinvoices.convertstreamtostring(in);                 log.i("main", "p2db - connect response = "+message);             }         }         catch(exception e){             e.printstacktrace();             //createdialog("error", "cannot establish connection");         }     } } 

here testpost.php page if like:

<?php     echo "\r\n<pre>\r\n";     var_dump("\$_post = ", $_post)."\r\n";     echo '$_post[\'email\'] = '.$_post['email']."\r\n";     var_dump("\$_server = ", $_server)."\r\n";     echo '</pre>';     die;  ?>   

from can see, httppost.setentity sets body of request without name/value pairings, raw post data. $_post doesn't raw data, name value pairs, converts hashtable/array. have 2 choices ... either process raw post data, or format request such includes name value pairs.

android/java, name value pair example:

httpclient httpclient = new defaulthttpclient();   httppost httppost = new httppost("http://web-billings.com/testpost.php");    list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);   namevaluepairs.add(new basicnamevaluepair("jsondata", se));   httppost.setentity(new urlencodedformentity(namevaluepairs));   

raw post data access in php:

$json = file_get_contents('php://input'); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -