从android应用程序发送到PHP页面的数据为空

从android应用程序发送到PHP页面的数据为空

本文介绍了从android应用程序发送到PHP页面的数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个android应用程序,它与php页面通信,将数据插入mysql表。它在Windows服务器上运行良好。最近,由于其他原因,我们被迫将应用程序移动到Linux服务器。之后会返回一些错误。当我交叉检查每个东西时,没有从android应用程序收到数据到php页面。

Android使用post方法发送数据。内容类型为application / x-www-form-urlencoded。数据使用UTF-8格式编码。



我可以在Android应用中正确看到变量值。但是当我们在Php页面中获取它时,返回null。当我们在php页面中分配一个值时,它可以工作



相同的代码在Windows服务器上运行良好。所以我得出结论,问题在于服务器设置。

服务器的默认字符集不是UTF-8,我要求服务器团队设置它,并且他们做了相同的添加以下代码.htaccess文件



// .htaccess代码



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/mydom/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"

## 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 mod_php5.c="">
  php_value default_charset "UTF-8"

<ifmodule mod_php5.c="">
  php_value safe_mode off

<ifmodule mod_php4.c="">
  php_value safe_mode off





还有其他任何设置在.htacess中更改或添加以使其工作





Android代码





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






Php code

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

//the above code  returns null

$cdt = json_decode($cdt , true);

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

// the above code returns null

$idt = json_decode($idt , true);



请为此建议解决方案



提前致谢!!!



我的尝试:



我尝试将服务器的默认字符集更改为UTF-8。但没有工作


Please suggest a solution for this

Thanks in Advance!!!

What I have tried:

I have tried changing default charset of server to UTF-8. but did not work

推荐答案






有没有其他设置需要更改或添加.htacess才能让它工作





Android代码





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






Php code



这篇关于从android应用程序发送到PHP页面的数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:19