Jump to content

Send data to PHP page POST method from android application


sirajulmuneer

Recommended Posts

Hi all,

 

I have an android application which communicates with a php page to insert data into mysql table. it was functioning well on an windows server. Recently we were forced to move the application to a linux sever for some other reason. After that some error is returned. When I cross checked every thing, no data is received from the android application to the php page.

Android send data using post method. Content type is "application/x-www-form-urlencoded". Data is encoded using UTF-8 format.

 

I can see the variables values correctly in android app. But when we fetch it in Php page "null" is returned. When we assign a value in php page it works

 

The same code was working finely on windows server. So I have concluded that problem was with the server settings.

Default charset of server was not UTF-8 and I asked server team to set it so and they did the same adding the following code .htaccess file

 

// .htaccess code

 

suPHP_ConfigPath /home/vyapary/php.ini
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 week"
ExpiresByType image/jpeg "access 1 week"
ExpiresByType image/gif "access 1 week"
ExpiresByType image/png "access 1 week"
ExpiresByType text/css "access 1 week"
ExpiresByType application/pdf "access 1 week"
ExpiresByType application/x-javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 week"
ExpiresByType image/x-icon "access 1 week"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_php5.c>
php_value default_charset "UTF-8"
</IfModule>
<IfModule mod_php5.c>
php_value safe_mode off
</IfModule>
<IfModule mod_php4.c>
php_value safe_mode off
</IfModule>
Is there any other setting to be changed or added in .htacess to get it working
Android code
String pdts=jsonobj.toString();
String citms=citm.toString();

url = new URL("http://www.mydom.in/myphppage.php");

String pdtsenc=URLEncoder.encode(pdts, "UTF-8");
String citmsenc=URLEncoder.encode(citms, "UTF-8");
String param = "cdt=" + pdtsenc +
"&idt=" + citmsenc;
conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
Php code
$cdt = urldecode($_POST['cdt']);
//this returns null
$cdt = json_decode($cdt , true);
$idt = urldecode($_POST['idt']);
// this returns null
$idt = json_decode($idt , true);
Please suggest a solution for this
Thanks in Advance!!!

 

Link to comment
Share on other sites

json_decode() returns null if the input string is not valid JSON. You should print out the string and see what's wrong with it.

 

urlldecode() should not be necessary because the content is already decoded before being put into the $_POST array.

Link to comment
Share on other sites

$cdt = urldecode($_POST['cdt']);

 

$idt = urldecode($_POST['idt']);

 

the above code is what returns null

 

if we assign the following data respectively to cdt and idt and decode it using json_decode it works

 

cdt="%7B%22uid%22%3A%2220%22%2C%22grtot%22%3A%2285.0%22%2C%22billamt%22%3A%2270.0%22%2C%22dpmd%22%3A%22Cash+On+Delivery%22%2C%22dname%22%3A%22ggh%22%2C%22daddress%22%3A%22ggg%22%2C%22delchg%22%3A%2215.0%22%2C%22dlocn%22%3A%22+Arakurushi+Ambalam%22%2C%22dtype%22%3A%22Normal%22%2C%22dumob%22%3A%229446127497%22%2C%22dlmark%22%3A%22ghh%22%2C%22dmob%22%3A%2288855%22%7D"

 

idt="%5B%7B%22cr_itemrate%22%3A%220%22%2C%22cr_offerrate%22%3A%2270%22%2C%22cr_total%22%3A%2270%22%2C%22cr_img%22%3A%22it_1173.jpg%22%2C%22cr_itemname%22%3A%22Crab+%28Njandu%29+1+Kg+%22%2C%22cr_qty%22%3A%221%22%2C%22cr_itemid%22%3A%221178%22%2C%22cr_slno%22%3A%221%22%7D%5D"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...